How does initializing fields in constructors work?

6

In the C # documentation it is written:

  

If a class does not have a constructor, a default constructor is automatically generated and default values are used to initialize the object fields.

That is, if a class does not have a constructor, a default constructor is automatically generated and default values are assigned to the fields of the class. But, and if I declare a parameterless constructor (which I believe is a default constructor), it looks like it still initializes the fields to their default value, for example:

    public class SimpleClass
    {
        public int Number { get; private set; }

        public SimpleClass()
        {

        }

        public void Increment()
        {
            Number++;
        }

    }


    static void Main(string[] args)
    {
        SimpleClass sc = new SimpleClass();
        sc.Increment();
        Console.WriteLine(sc.Number); // Mostra: 1
        Console.ReadKey();
    }

But this is invalid:

    int i;
    i++;

Why?

    
asked by anonymous 01.02.2016 / 20:39

3 answers

7

Perhaps the best way to see this is to look at the CIL code:

.assembly extern mscorlib
{
  .publickeytoken = (B7 7A 5C 56 19 34 E0 89 )                         // .z\V.4..
  .ver 4:0:0:0
}
.assembly '510a4553-ce16-420a-a407-708c596fefd2'
{
  .hash algorithm 0x00008004
  .ver 0:0:0:0
}
.module '510a4553-ce16-420a-a407-708c596fefd2.dll'
// MVID: {CA762240-7FEE-4C94-8DA0-A08E521CA6D0}
.imagebase 0x10000000
.file alignment 0x00000200
.stackreserve 0x00100000
.subsystem 0x0003       // WINDOWS_CUI
.corflags 0x00000001    //  ILONLY
// Image base: 0x01140000


// =============== CLASS MEMBERS DECLARATION ===================

.class public auto ansi beforefieldinit Program
       extends [mscorlib]System.Object
{
  .method public hidebysig static void  Main(string[] args) cil managed
  {
    // 
    .maxstack  1
    .locals init (class SimpleClass V_0)
    IL_0000:  nop
    IL_0001:  newobj     instance void SimpleClass::.ctor()
    IL_0006:  stloc.0
    IL_0007:  ldloc.0
    IL_0008:  callvirt   instance void SimpleClass::Increment()
    IL_000d:  nop
    IL_000e:  ldloc.0
    IL_000f:  callvirt   instance int32 SimpleClass::get_Number()
    IL_0014:  call       void [mscorlib]System.Console::WriteLine(int32)
    IL_0019:  nop
    IL_001a:  call       valuetype [mscorlib]System.ConsoleKeyInfo [mscorlib]System.Console::ReadKey()
    IL_001f:  pop
    IL_0020:  ret
  } // end of method Program::Main

  .method public hidebysig specialname rtspecialname 
          instance void  .ctor() cil managed
  {
    // 
    .maxstack  8
    IL_0000:  ldarg.0
    IL_0001:  call       instance void [mscorlib]System.Object::.ctor()
    IL_0006:  nop
    IL_0007:  ret
  } // end of method Program::.ctor

} // end of class Program

.class public auto ansi beforefieldinit SimpleClass
       extends [mscorlib]System.Object
{
  .field private int32 '<Number>k__BackingField'
  .method public hidebysig specialname instance int32 
          get_Number() cil managed
  {
    // 
    .maxstack  1
    .locals init (int32 V_0)
    IL_0000:  ldarg.0
    IL_0001:  ldfld      int32 SimpleClass::'<Number>k__BackingField'
    IL_0006:  stloc.0
    IL_0007:  br.s       IL_0009

    IL_0009:  ldloc.0
    IL_000a:  ret
  } // end of method SimpleClass::get_Number

  .method private hidebysig specialname instance void 
          set_Number(int32 'value') cil managed
  {
    // 
    .maxstack  8
    IL_0000:  ldarg.0
    IL_0001:  ldarg.1
    IL_0002:  stfld      int32 SimpleClass::'<Number>k__BackingField'
    IL_0007:  br.s       IL_0009

    IL_0009:  ret
  } // end of method SimpleClass::set_Number

  .method public hidebysig specialname rtspecialname 
          instance void  .ctor() cil managed
  {
    // 
    .maxstack  8
    IL_0000:  ldarg.0
    IL_0001:  call       instance void [mscorlib]System.Object::.ctor()
    IL_0006:  nop
    IL_0007:  nop
    IL_0008:  ret
  } // end of method SimpleClass::.ctor

  .method public hidebysig instance void 
          Increment() cil managed
  {
    // 
    .maxstack  3
    .locals init (int32 V_0)
    IL_0000:  nop
    IL_0001:  ldarg.0
    IL_0002:  call       instance int32 SimpleClass::get_Number()
    IL_0007:  stloc.0
    IL_0008:  ldarg.0
    IL_0009:  ldloc.0
    IL_000a:  ldc.i4.1
    IL_000b:  add
    IL_000c:  call       instance void SimpleClass::set_Number(int32)
    IL_0011:  nop
    IL_0012:  ret
  } // end of method SimpleClass::Increment

  .property instance int32 Number()
  {
    .get instance int32 SimpleClass::get_Number()
    .set instance void SimpleClass::set_Number(int32)
  } // end of property SimpleClass::Number
} // end of class SimpleClass

Especially:

  .method public hidebysig specialname rtspecialname 
          instance void  .ctor() cil managed
  {
    // 
    .maxstack  8
    IL_0000:  ldarg.0
    IL_0001:  call       instance void [mscorlib]System.Object::.ctor()
    IL_0006:  nop
    IL_0007:  ret
  } // end of method Program::.ctor

The compiler generated a method called .ctor . It is a non-static instance method handled in a special way by the CLR and it always calls the constructor of the ascending class, in the case Object .

But if you have a static constructor, then it will be called .cctor and will still be static (and necessarily private:

  .method private hidebysig specialname rtspecialname static 
          void  .cctor() cil managed
  {
    // 
    .maxstack  8
    IL_0000:  nop
    IL_0001:  ret
  } // end of method Program::.cctor

In a creator constructor by the programmer, it would have other code there and could even assign values to members of the class. Regardless of this there is member initialization occurring during construction (performed by the independent CLR and before the constructor, as can be seen in another question about the theme .

Note that there is a constructor in both classes. In general, a class that will only have static members can be declared as static and avoid the existence of the instance constructor.

    
01.02.2016 / 21:39
4

Because your class also gains an implicit constructor, nicknamed ctor . It is created when there are no constructors defined by the programmer.

I do not know if you ever had this idea of putting a breakpoint in SimpleClass sc = new SimpleClass(); and pressing F11 . The next line of execution will fall here:

public int Number { get; private set; }

That is, regardless of whether you have an empty constructor or not, the class needs to initialize the property in one form or another.

At this initialization, some values are set by default when the programmer does not inform them. Here you can see all of them .

    
01.02.2016 / 20:46
0

In C # everything has 2 data types the
reference types: referency type These types are given null default values,
val types: value type ex: int, char, bool. these have a default value, predefined, and the value of int is 0 In this case: int i; i ++; of a compilation error that a form of compiler prevention, but in another case already they are in run time and there the properties receive all their default values, therefore not of the error,

Another thing is that the int and all value type are non nullable type ie they are never null.

    
02.02.2016 / 10:50