less than 1 minute read

The next release of C# 6 has some amazing new features. In a series of blog posts I will cover some of them.

At first auto-property initializers does not sound very interesting at all, but wait…

Simple things as setting a default value for a property.

public class Order
{
  public int OrderNo { get; set; } = 1;
}

Or using the getter-only auto-property which are implicit declared readonly and can therefore be set in the constructor.

public class Order
{
  public Order(int orderNo)
  {
    OrderNo = orderNo;
  }

  public int OrderNo { get; }
}

From my point of view the value of auto-properties comes to shine when used with list properties where the list has to be initialized.

public class Order
{
  public IEnumerable<OrderLine> Lines { get; } = new List<OrderLine>();
}

I often forget to initialize a list property in the constructor and therefor get a NullReferenceException when accessing the list property. Now I might even be able to omit the constructor all together.

Tags: ,

Categories:

Updated:

Comments