Who will initialize data members? Is it a constructor or something else?

If this is the default constructor, who will initialize member variables to zero, then how will it be possible

class A { public int i; public int j; public A() { i=12; } } class Program { static void Main(string[] args) { A a = new A(); Console.WriteLine(ai + "" + aj); Console.ReadLine(); } } 
+7
c #
source share
6 answers

Because field initializers are run before the constructors. You have not assigned the value j in your constructor, it will be 0 by default, since it is int .

From 10.4.4 Field Initialization

The initial value of the field, whether it is a static field or an instance field, is the default value ( Section 5.2 ) of the type field.

Who is doing this? Well, it looks like newobj does this when you call new . See dyatchenkos answer for more details.

+19
source share

Field initialization occurs before the constructor is called through the newobj command. It is easy to verify after decompiling the executable with the following C # code:

 using System; namespace ConsoleApplication1 { public class A { public int j; } class Program { static void Main(string[] args) { A g = new A(); Console.WriteLine(gj); } } } 

Part of the decompiled MSIL code (main method):

 //000017: { IL_0000: nop //000018: A g = new A(); IL_0001: newobj instance void ConsoleApplication1.A::.ctor() IL_0006: stloc.0 //000019: Console.WriteLine(gj); IL_0007: ldloc.0 IL_0008: ldfld int32 ConsoleApplication1.A::j IL_000d: call void [mscorlib]System.Console::WriteLine(int32) IL_0012: nop //000020: } 

As we can see, MSIL uses the newobj to create an instance of class A. According to the following microsoft acticle :

The newobj command selects a new instance of the associated class with ctor and initializes all fields in the new instance to 0 (of the correct type) or null references, as appropriate. He then calls the ctor constructor with the given arguments along with the newly created example. After calling the constructor, an object reference (type O) is now initialized and pushed onto the stack.

If this is not correct, comment, otherwise designate it as the correct answer.

+11
source share

In C #, int variables will always default to 0 unless otherwise specified. In your constructor:

 public A () { i=12; } 

When your object is created, your property i will be initialized to 12, and your property j will be initialized to 0.

In your code:

 public class Program { A a = new A(); // You instantiated your object, i will be assigned 12 Console.WriteLine(aiToString() + "" + ajToString()); // Your output will be 12 0 because you didn't initialize j so the default is 0 Console.ReadLine(); } 

Hope this helps!

+2
source share

The answer is that the default number of C # fields is 0. While in C ++, the default int value is some random value.

+1
source share

During instance creation, its variables also become available. And since int is not null, it initializes by default (int). Which 0

+1
source share

C # set the default int value to 0 . More details about the default values ​​are found here.

And further, since they are fields, this applies

The initial value of the field, whether it is a static field or an instance field, is the default value.

The default constructor will assign i 12 , but j will have 0 as the default value, since it was not assigned anywhere in the constructor.

+1
source share

All Articles