The Top 5 New Features in C#6.0

Visual Studio 2015 recently introduced a new version of C# named C#6.0. I would not say it has major features added like Extension Method or LINQ in C# 3.0 or Async/ Await in C# 5.0. However it has many features that simplify the syntaxes and improve the readability of the code. Major C# 6.0 features include:

  • Initializers for auto-properties
  • Getter-only auto-properties
  • Expression-bodied function members
  • Using the static statement
  • Null-conditional operators
  • String interpolation
  • nameof expressions
  • Index initializers
  • Exception filters
  • Await in catch and finally blocks
  • Extension Add methods in collection initializers etc.

In this post, I will discuss the top 5 five features of C# 6.0, offcousre according to me

Auto-property initializers

In C# 6.0, a new feature has been added to set the default values of the public properties, called Auto-property initializers. Using the auto-property initializers, we can set default values to the properties without having to use a constructor. Let us consider a simple example of the Author class as shown in the listing below:

image

As you notice in the above listing, the default value of the Articles property is set to 10. So while creating the object, if the value of the Articles property is not set then it would be set to the default value 10. In the below listing, we are not setting the value of the Articles property of the object a, hence it is set to the default value of 10.

image

The auto-property initializer sets the value of the property directly into the backing field without invoking the setters. Prior to C# 6.0, we used to create read only properties by creating a private setter and then setting the value in the constructor. However in C#6.0, using the auto-property initializer, a read only property can be easily created as shown in the listing below:

image

Read the full article on the Infragistics blog

Leave a comment

Create a website or blog at WordPress.com