1

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?

CMS
  • 3,657
  • 1
  • 27
  • 46

3 Answers3

6

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.

Habib
  • 219,104
  • 29
  • 407
  • 436
3

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 !

Christos
  • 53,228
  • 8
  • 76
  • 108
-1

There's one use case not covered by the other answer that is when assigning the new object to a class or struct field:

public class MyClass
{
    private MyObject m;

    public void MyMethod()
    {
        this.m = new MyObject { MyNumber = 8 };
    }
}

Because that field might be in use outside of the class and the statement looks atomic to the user, the compiler generates a temporary variable and only assigns a value to the field when completely initialized:

var temp = new MyObject();
temp.MyNumber = 8;
this.m = temp;
Paulo Morgado
  • 14,111
  • 3
  • 31
  • 59
  • This is irrelevant to the actual question asked. If you would like to comment on another answer, then you should post a comment; you should not be posting an answer when not even attempting to answer the quesiton. – Servy Jul 28 '16 at 20:54