8

Let's say I'm doing this...

public class Customer
{
    public Customer()
    {
        customerID = Guid.NewGuid();
    }

    public Guid customerID { get; set; } = Guid.NewGuid();
}

...because, I know this is going to occur as my dev team moves to C#6.0. Which is going to take precedence / be used (the constructor NewGuid() or the property NewGuid()) and why? When should we use one vs. the other as a best practice?

Ɖiamond ǤeezeƦ
  • 3,223
  • 3
  • 28
  • 40
maplemale
  • 2,006
  • 21
  • 35
  • I prefer the constructor method because it allows for overloading and a quick view of what's going on. None of the large systems I've worked on use the auto-initializers. – Chris Fannin May 10 '16 at 20:57
  • Did you try changing the type to a non-guid and just running the code? – Derek Tomes May 10 '16 at 21:00
  • 4
    It would take you less time than it took you to write this question to just run the code and find out for yourself. – Servy May 10 '16 at 21:01
  • 2
    My question is not simply regarding order, but also the "why" and best practices. And whether or not it would take me longer to write a test and check for myself or not, is irrelevant. Maybe I'm just trying to add value to the StackOverflow community since I couldn't find this answer in a quick search myself? – maplemale May 10 '16 at 21:04
  • Read [here](http://stackoverflow.com/a/1882778/5095502) for some "why", especially the links in the top rated comment. The only thing missing is that the auto-property initializer expands to the code posted by [Evk](http://stackoverflow.com/a/37148877/5095502). – Quantic May 10 '16 at 21:18

3 Answers3

20

Auto property initializer is basically syntax sugar for this:

private Guid _customerID = Guid.NewGuid();
public Guid customerID
{
    get { return _customerID; }
    set { _customerID = value; }
}

So actual question is: what runs first, field initializers or constructor. And the answer is long time known - field initializers run first, then constructor. So value written in constructor will overwrite value from field initializer in your example.

Evk
  • 98,527
  • 8
  • 141
  • 191
  • Nice observation. I was just googling about this subject, but couldn't find anything. – Xiaoy312 May 10 '16 at 21:08
  • You can see with decompiler that it works like this for example. – Evk May 10 '16 at 21:10
  • I agree with what your answer. I was just surprised, as there is no article on the internet about the auto-property initializer's execution order. It turns out, I had forgotten what they expanded to. – Xiaoy312 May 10 '16 at 21:21
5

It seems that the Auto-property initializer will be the first to be invoked, and then, the constructor.

I made a small demo with LINQPad :

void Main()
{
    new Test().Dump("result");
}

// Define other methods and classes here
class Test
{
    public int MyProperty1 { get; set; } = 1.Dump("auto-prop init");
    public Test()
    {
        MyProperty1 = 2.Dump(".ctor");
        MyProperty2 = 2.Dump(".ctor");
    }
    public int MyProperty2 { get; set; } = 1.Dump("auto-prop init");
}

And, the result is the following :

enter image description here

Xiaoy312
  • 14,292
  • 1
  • 32
  • 44
2

It looks like Autoproperty then constructor. Running this shows 1 as the value which I assume the constructor overwriting the initial value.

public class Customer
{
    public Customer()
        {
            customerID = 1;
        }
    public int customerID { get; set; } = 2;
}
MichaelChan
  • 1,808
  • 17
  • 34