Static Constructor

  • Thread starter Thread starter Chris B.
  • Start date Start date
C

Chris B.

In C#, is a static constructor guaranteed to get called before any static
method or property of a class is called even if an instance of that class is
never created?

Thanks,
Chris
 
Chris,

Yes it is. A static constructor is called before any instance is
created, or static property and/or method is instantiated.

Hope this helps.
 
Hi,

I think that a note should be made regarding static variables that are
declared and given a value at the same time like in this case:

static int Max = 40;

they are called first, take a look at Jon's article about it:
http://www.yoda.arachsys.com/csharp/beforefieldinit.html

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


Nicholas Paldino said:
Chris,

Yes it is. A static constructor is called before any instance is
created, or static property and/or method is instantiated.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Chris B. said:
In C#, is a static constructor guaranteed to get called before any static
method or property of a class is called even if an instance of that
class
is
never created?

Thanks,
Chris
 
Back
Top