Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C#, is a default constructor generated when class members are initialized?

Suppose I initialize members of a class like this:

class A 
{
    public int i=4;
    public double j=6.0;
}

Does the compiler generate a default constructor in this situation?

In general, I know that a constructor may initialize the value of class instance variables and may also perform some other initialization operations appropriate for the class. But in the above example, I have initialized the value of i and j outside of a constructor. In this situation, does the compiler still generate a default constructor? If so, what does the default constructor do?

like image 557
Nishant Kumar Avatar asked Sep 06 '10 05:09

Nishant Kumar


People also ask

What is '~' in C language?

The tilde (~) is a character in the standard ASCII character set that is provided on a conventional computer keyboard and is used in both writing and computer programming. It corresponds to ASCII code 126. The tilde is also sometimes known as the twiddle.

What does += mean in C?

+= Add AND assignment operator. It adds the right operand to the left operand and assign the result to the left operand. C += A is equivalent to C = C + A. -=

What is operators in C?

An operator is a symbol that tells the compiler to perform specific mathematical or logical functions. C language is rich in built-in operators and provides the following types of operators −


2 Answers

The compiler still generates a default constructor in this case. The constructor handles the initialization of i and j. If you look at the IL this is evident.

.class auto ansi nested private beforefieldinit A
   extends [mscorlib]System.Object
{
   .method public hidebysig specialname rtspecialname instance void .ctor() cil managed
   {
      .maxstack 8
      L_0000: ldarg.0 // pushes "this" onto the stack
      L_0001: ldc.i4.4 // pushes 4 (as Int32) onto the stack
      L_0002: stfld int32 TestApp.Program/A::i // assigns to i (this.i=4)
      L_0007: ldarg.0 // pushes "this" onto the stack
      L_0008: ldc.r8 6 // pushes 6 (as Double) onto the stack
      L_0011: stfld float64 TestApp.Program/A::j // assigns to j (this.j=6.0)
      L_0016: ldarg.0 // pushes "this" onto the stack
      L_0017: call instance void [mscorlib]System.Object::.ctor() // calls the base-ctor
      /* if you had a custom constructor, the body would go here */
      L_001c: ret // and back we go
   }
like image 193
Brian Rasmussen Avatar answered Oct 06 '22 12:10

Brian Rasmussen


You can read these things up in the official ECMA language standard. Chapter 17.4.5 talks about this specific issue, basically stating that fields will be default-initialized with whatever default value the type has (0 or 0.0, respectively in your case), and afterwards the value initialization will be executed in the order that they are declared in the source file.

like image 32
Jim Brissom Avatar answered Oct 06 '22 12:10

Jim Brissom