C#, immutability and public readonly fields

C# 6.0 now supports auto-property initializers.

The auto-property initializer allows assignment of properties directly
within their declaration. For read-only properties, it takes care of
all the ceremony required to ensure the property is immutable.

You can initialize read-only properties in constructor or using auto-initializer

public class Customer
{
    public Customer3(string firstName, string lastName)
    {
        FirstName = firstName;
        LastName = lastName;
    }
    public string FirstName { get; }
    public string LastName { get; }
    public string Company { get; } = "Microsoft";
}

var customer = new Customer("Bill", "Gates");

You can read more about auto-property initializers here

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)