Skip to main content
CSharp Initialization Sequence of Members

Initialization Sequence Within a Single Instance

Consider a part of CSharp code as follows, and try to guess what is the value of instance.Number:


var instance = new SampleClass()
{
    Number = 3;
};

Console.WriteLine(instance.Number); // What's the value of property 'Number'?

class SampleClass
{
    public int Number {get; set;} = 1;

    public SampleClass()
    {
        Number = 2; 
    }
}

Haoyu JiaOriginalAbout 2 minCSharpFundamentals