c# - Class variables not initializing -
i have bizarre problem seems sort of compiler error, feel that's unlikely.
i have 2 nested classes both have class variables. 1 of them (spritelist
) works fine , initializes of variables perfectly. when breakpoint it, program break when initializing class.
the other nested class (itemlist
) acting little weird. class variable in isn't initializing , when breakpoint code, program never breaks.
this code:
class registry { public static dictionary<string, staticsprite> spriteregistry = new dictionary<string, staticsprite>(); public static dictionary<string, item> itemregistry = new dictionary<string, item>(); public static void registersprite(string name, staticsprite sprite) { spriteregistry.add(name, sprite); console.writeline("registered sprite: " + name + "!"); } public static void registeritem(string name, item item) { itemregistry.add(name, item); console.writeline("registered item: " + name + "!"); } public class itemlist { public static item test = new itemtest(); } public class spritelist { public static staticsprite rock = new spriterock(); public static staticsprite pedestal = new spritepedestal(); public static staticsprite item = new spriteitem(); } }
i have no idea wrong here. neither class constructed anywhere, not should matter. when move variables in itemlist spritelist, runs fine.
i have no clue what's wrong here.
without a good, minimal, complete code example reliably reproduces problem, it's impossible know sure problem is.
but note runtime not required initialize class until class used @ runtime. is, @ point in code executes needs class.
a breakpoint in static initialization (e.g. static constructor, method called static initializer, etc.) may never hit, , code never executed, if class code called never used in program.
it seems me that's issue here. if there no code ever uses itemlist
class, there's no need static
field in class initialized, , so…it isn't ever initialized.
Comments
Post a Comment