Person.Junkie
Person.Junkie

Reputation: 1886

Do both of these constructors do the same thing?

Do both of these code blocks do the same thing?

class A {
   public static int s;
   A(){}
   static A(){s = 100;}
}

and

class A {
   public static int s=100;
   A(){}
   //static A(){s = 100;} do not use
}

Do they do the same thing? I think so.

Upvotes: 4

Views: 183

Answers (4)

Mehdi Khademloo
Mehdi Khademloo

Reputation: 2812

According to C# in a Nutshell the static constructor is executed when you first use of the property, and the most usage of this is when you want to calculate or read some data and set to the static property (Like a password from appconfig.config). without using the static constructor you must define a Init method and call it when your application has been started.

Upvotes: 0

D Stanley
D Stanley

Reputation: 152606

The effect is the same, but the actual order of execution may be different. When there is a static constructor, static fields are initialized immediately before the constructor is called or any of the static fields are accessed. If there is no static constructor, then the field initializers can be executed at any time prior to the first usage of one of the static fields.

Since your initializers have no side-effects and cannot throw exceptions, there would not be any discernable difference in the two, barring the use of reflection or some other outside observer (e.g. debugger)

Upvotes: 4

Tim S.
Tim S.

Reputation: 56556

Except for some edge cases such as beforefieldinit behavior, yes, they do the same thing. In fact, the compiled IL is nearly the same in the two cases. The only difference is the presence of beforefieldinit on the one without a static constructor.

class A
{
    public static int s;
    A() { }
    static A() { s = 100; }
}

class B
{
    public static int s = 100;
    B() { }
}

Compiles to...

.class private auto ansi A
    extends [mscorlib]System.Object

.class private auto ansi beforefieldinit B
    extends [mscorlib]System.Object

With identical method/field definitions.

Upvotes: 3

Jon Skeet
Jon Skeet

Reputation: 1502296

No, they don't behave quite the same way. Without the static constructor, the timing of exactly when the type initializer executes is much looser - it can happen earlier or later than you'd expect.

When there's a static constructor, the type initializer executes when the type is first used in terms of any static member being accessed or any instance being created.

When there isn't a static constructor, the only guarantee is that the initializer will be executed at some point before the first access of a static field (and still exactly once). Depending on the JIT, that might mean it's executed very early (e.g. when you first execute a method which might use a member) or very late (after calling static members which don't use any fields, or after creating and using an instance).

In IL, the difference is that a type without a static constructor has the beforefieldinit flag; one with a static constructor doesn't.

Upvotes: 11

Related Questions