Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the first argument in a parameterless constructor?

I have simple program like this:

public class Foo
{
    public Foo()
    {
    }
    public int MyInt { get; set; } = 10;
    public List<int> MyList { get; set; } = new List<int>();
}

public class Program
{
    static public void Main()
    {
        Console.WriteLine(new Foo().MyInt);
        Console.ReadLine();
    }
}

I decided to see the CIL code of such program (I am interested in Foo's constructor). Here is it:

.method public hidebysig specialname rtspecialname
        instance void  .ctor() cil managed
{
    // Code size       26 (0x1a)
    .maxstack  8
    IL_0000:  ldarg.0
    IL_0001:  ldc.i4.s   10
    IL_0003:  stfld      int32 Foo::'<MyInt>k__BackingField'
    IL_0008:  ldarg.0
    IL_0009:  newobj     instance void class [mscorlib]System.Collections.Generic.List`1<int32>::.ctor()
    IL_000e:  stfld      class [mscorlib]System.Collections.Generic.List`1<int32> Foo::'<MyList>k__BackingField'
    IL_0013:  ldarg.0
    IL_0014:  call       instance void [mscorlib]System.Object::.ctor()
    IL_0019:  ret
} // end of method Foo::.ctor

I wondered, when I saw the second line, ldarg.0, what does it mean? this pointer? But the object was not created yet. How can I modify its members? My assumption is that before calling constructor, clr first allocates memory for the object. Then initializes members to default values, and then invokes the constructor. Another interesting moment that the object calling is last. I thought that it would be first.

like image 968
LmTinyToon Avatar asked Feb 08 '17 08:02

LmTinyToon


People also ask

What is a Parameterless constructor?

A constructor that takes no parameters is called a parameterless constructor. Parameterless constructors are invoked whenever an object is instantiated by using the new operator and no arguments are provided to new .

How do you create a Parameterless constructor in Java?

Example 1: Creating a Parameter-Less Constructor The following code shows how to use a parameter-less constructor. It was mentioned before that the constructor method name will be the same as the class name. Here, the class name is 'con1,' so the parameter-less constructor name is 'con1().

Which constructor is called first in C#?

In C# terms, the base constructor is executed first.

What is base in C# constructor?

base (C# Reference) The base keyword is used to access members of the base class from within a derived class: Call a method on the base class that has been overridden by another method. Specify which base-class constructor should be called when creating instances of the derived class.


1 Answers

Field initializers are a C# feature, not a CLR one. When you write a field initializer, the C# compiler has to put the code to implement that somewhere, and where it puts it is inside the body of any constructors.

And since these initializers are run "before" the constructor, that's why the actual base-class constructor is run later.

(And so, yes, the first parameter is as you inferred, this)

like image 197
Damien_The_Unbeliever Avatar answered Oct 23 '22 05:10

Damien_The_Unbeliever