Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats called first - Object Initializer or Construtor?

Tags:

c#

c#-6.0

I was wondering whats gets called first lets say i have this code

public class MyObject
{
    public MyObject()
    {
        MyNumber = 6;
    }
    public int MyNumber { get; set; }
}

and then i call it

var o = new MyObject {MyNumber = 8 };

What will MyNumber be - 6 or 8 ?

And lets say i have set this too

public int MyNumber { get; set; } = 7;

whats the order? what takes presents?

like image 797
CMS Avatar asked Jun 01 '26 17:06

CMS


2 Answers

Your statement:

var o = new MyObject {MyNumber = 8 };

is equivalent to somewhat

var o = new MyObject();
o.MyNumber = 8;

so the result you will get back is 8, since any value assigned to MyNumber will be overridden due to object initialization.

With property initializer it would be the same result, the only difference is that:

  • At the time of constructor call, the property MyNumber will have the default value of 7
  • In the constructor it would be assigned 6
  • But due to Object initializer it will be assigned 8

In case of object intializer it is the constructor that is called first.

How to: Initialize Objects by Using an Object Initializer (C# Programming Guide)

The compiler processes object initializers by first accessing the default instance constructor and then processing the member initializations. Therefore, if the default constructor is declared as private in the class, object initializers that require public access will fail.

like image 144
Habib Avatar answered Jun 03 '26 05:06

Habib


What will MyNumber br 6 or 8 ?

It will be 8. When the following line would be executed:

var o = new MyObject {MyNumber = 8 };

an object of type MyObject would be created and afterwards the value of MyNumber would be set to 8. Under the hood the default constructor would be used for the creation of the object. So before MyNumber would be set to 8, it would be 6.

and whats the story with the 7 i just added?

It is the same as for the above case. Now, you are in the case of an auto property initializer (a feature that has been introduced in C# 6). The following

public int MyNumber { get; set; } = 7;

is equivalent as to initialize the value of MyNumber inside the default constructor.

If you create a console application with the following code:

public class MyObject
{
    public MyObject()
    {
        MyNumber = 6;
    }
    public int MyNumber { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        var o = new MyObject { MyNumber = 8 };
    }
}

and then compile the code and decompile it using ildasm, you will get the following picture, if you look at the IL code of Main.

enter image description here

From the first underlined line it's clear that the default object initializer is called. Then at the second underline line the value of a 8 is set to the corresponding backing field.

Furthermore, If you compile the code for the following console app:

public class MyObject
{
    public int MyNumber { get; set; } = 8;
}

class Program
{
    static void Main(string[] args)
    {
        var o = new MyObject { MyNumber = 8 };
    }
}

you will get exactly the same IL code !

like image 28
Christos Avatar answered Jun 03 '26 06:06

Christos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!