How to do it...

  1. 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";
  1. 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"; ...

Get ASP.NET Core MVC 2.0 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.