In this recipe, we will discover some of the new features in C# 6.
- Feature one—auto initializer for properties:
Before C# 6, we should initialize a property with a value in the class constructor. There is no point in time before the class constructor, so we should use it. Thanks to C# 6 syntax, we can now initialize a property with a value right on the declaration line. No need to use constructor anymore:
- Before:
public class Person { Public class Name {get;set;} Public Person(){ Name = "Stephane"; } }
- Now:
Public string Name {get;set;} = "Stephane";
- Feature two—auto initializer for static properties:
Auto initializers can be applied to static properties, as well:
- Before:
Public static string Name {get;set;}
- Now:
Public static string Name {get;set;} = "Stephane";
- Feature three—read-only properties:
Auto initializers can even be applied to read-only properties:
- Before:
public List SocialNetworks { get; }
- Now:
public List SocialNetworks { get; } = new List { "Twitter...