c# - Is a static collection guaranteed to be initialized/populated before another class uses it? -


suppose have these 2 classes:

public class a<t> t : ientitywithid, new() {     private static entityinfo entityinfo = b.getentityinfo(typeof(t));      private static ienumerable testcases     {         // work entityinfo...     } }  private static class b {     private static ilist<entityinfo> entityinfolist = b.getentitylist();      public static entityinfo getentityinfo(type type)     {        return entityinfolist.single(e => e.types.contains(type));     }      private static ilist<entityinfo> getentitylist()     {        // builds list of entityinfo's...     } } 

is entityinfolist in class b guaranteed initialized , populated before b.getentityinfo() called in class a?

yes, guaranteed. here's snippet msdn:

the program cannot specify when class loaded. however, guaranteed loaded , have fields initialized , static constructor called before class referenced first time in program.

edit: pointed out, end in situation 2 static classes depend on each other initialization in trouble, long that's not case, you're fine.


Comments