New in C# version 6.0 – Auto-Property Initializers


In the past, we may have created our properties with a SET and GET and then initialized our constructor with the value that we wanted as shown below.

1
2
3
4
5
6
7
8
9
public class Customer
{
    public Customer()
    {
        customerID = Guid.NewGuid();
    }

    public Guid customerID { get; set; }
}

We can now reduce this code block to one line as shown below. No longer do we need to create a setter or constructor:

1
2
3
4
public class Customer
{
    public Guid customerID { get; set; } = Guid.NewGuid();
}

Nice to have!