c# - Initialize variables outside the constructor -


i want create small static class in c#.

this class needs list , bool.

what's difference between initializing list , setting default value of bool (false) outside of methods, that:

class myclass {     static bool = true;     static list<int> b = new list<int>(); } 

and initializing them inside method, that:

class myclass {     static bool a;     static list<int> b;      public static void initialize()     {         = true;         b = new list<int>();     } } 

in first approach, fields assigned values on first reference of static class.

in second approach, fields not assigned your values until call method initialize. assigned default values.

this different case if have static constructor class like:

class myclass {     public static bool a;     public static list<int> b;      static  myclass()     {         = true;         b = new list<int>();     } } 

if want initialize public fields can use static constructor called automatically when static members referenced. (please note in case, fields public)

see: static constructor - msdn

a static constructor used initialize static data, or perform particular action needs performed once only. called automatically before first instance created or static members referenced.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -