Auto Property Initializer: A New Feature of C# 6.0

Introduction

On November 12, 2014, the day of the Visual Studio Connect() event, Microsoft announced Visual Studio 2015 Preview with many new and exciting features for developers for testing purposes. Microsoft announced the new version of C#, C# 6.0 that came with many improvements and new features. One of the newly introduced features of C# 6.0 is Auto Property Initializer.

Don't forget to read my previous posts on this series: "A new feature of C# 6.0"

What is Auto Property Initializer

The Auto Property feature was first added to the C# language with the release of C# 3.0 on 19 November 2007 as a part of .NET Framework 3.5 that allows us to define the property without any backing field, however we still need to use constructors to initialize these auto properties to a non-default value. With the release of C# 6.0, a new feature called Auto Property Initializer allows us to initialize these properties without a constructor. In C# 6.0, we can now initialize the properties where they are declared.

Example 1

The following code snippet defines a class Student with one auto property, Name, and a parameterized constructor has been defined to initialize this property to some value.

  1. public class Student  
  2. {  
  3.     public string Name { getset; } // auto property- Name //declaration  
  4.     public Student() // constructor  
  5.     {  
  6.         Name = "Akash"//assigment  
  7.     }  
  8. }  

Note that:

  • Both the declaration and assignment are in different places.

  • We can't have a "get" only auto-property.

In C# 6.0, the same code can be rewritten as shown below.

  1. public class Student  
  2. {  
  3.     public string Name  
  4.     {  
  5.        { getset: } = "Akash";  
  6.     }  
  7. }   

What is Getter Only Auto-Properties

Properties can be made read-only by removing the setter accessor. That can be done by having only a get accessor in the property implementation. With no setter, immutability is easier to do.

Example 2

The following code only has a getter accessor.

  1. public class point  
  2. {  
  3.     public int x { get; }  
  4.     public int y { get; }  
  5. }  

Example 3

To make the set accessor available only to code within the class, we can use the private access modifier. The property will appear to be read only when accessed from outside of the defined (where the property is defined with private set accessor) class.

  • When the declaration and assignment of the get accessor and set accessor are in a single class, we can easily initialize the values using setter accessor in that class.

    1. using System;  
    2. namespace CSharpFeatures  
    3. {  
    4.     public class AutoProperty  
    5.     {  
    6.         public string Name { getprivate set; } = "Amit Sharma";  
    7.         public static void Main(string[] args)  
    8.         {  
    9.             AutoProperty ap = new AutoProperty();  
    10.             ap.Name = "Abhishek Arora";  
    11.             Console.WriteLine("Name= \{ap.Name}"); // prints Abhishek Arora  
    12.             Console.ReadKey();  
    13.         }  
    14.     }  
    15. }  
  • When the declaration and assignment of the get accessor and set accessor are in a different class we are unable to initialize the values using a setter accessor in another class except where they are declared.

    1. using System;  
    2. namespace CSharpFeatures  
    3. {  
    4.     public class AutoProperty  
    5.     {  
    6.         public string Name { getprivate set; } = "Amit Sharma";  
    7.     }  
    8.     public class exec  
    9.     {  
    10.         public static void Main(string[] args)  
    11.         {  
    12.             AutoProperty ap = new AutoProperty();  
    13.             ap.Name = "Abhishek Arora";                      //Error   
    14.             Console.WriteLine("Name= \{ap.Name}");  
    15.             Console.ReadKey();  
    16.         }  
    17.     }  
    18. }  
    setter inaccessible 

Demo Application using Visual Studio 2013

  1. using System;  
  2. namespace CSharpFeatures  
  3. {  
  4.     public class Studentdetails  
  5.     {  
  6.         private string _name;  
  7.         public string Name   
  8.         {   
  9.             get {  return this._name;  }  
  10.             set {  this._name=value;   }  
  11.         }  
  12.         private string _address;  
  13.         public string Address   
  14.         {   
  15.             get {  return this._address;  }  
  16.             set {  this._address=value;  }  
  17.         }   
  18.   
  19.         public static void Main(string[] args)  
  20.         {  
  21.             Studentdetails Student1 = new Studentdetails();  
  22.             Student1.Name = "Amit Sharma";  
  23.             Student1.Address = "Delhi";  
  24.             Console.WriteLine("\n Student1");  
  25.             Console.WriteLine(" Name= {0}",Student1.Name);  
  26.             Console.WriteLine(" Address= {0}",Student1.Address);  
  27.               
  28.             Studentdetails Student2 = new Studentdetails  
  29.             {  Name = "Vijay Verma", Address = "New Delhi"  };  
  30.   
  31.             Console.WriteLine("\n Student2");  
  32.             Console.WriteLine(" Name= {0}", Student2.Name);  
  33.             Console.WriteLine(" Address= {0}", Student2.Address);  
  34.             Console.ReadKey();  
  35.         }  
  36.     }  
  37. }  

Output


Demo Application using Visual Studio 2015 Preview

  1. using System;  
  2. namespace CSharpFeatures  
  3. {  
  4.     public class Studentdetails  
  5.     {  
  6.         public string Name { getset; } = "Amit Sharma";  
  7.         public string Address { getset; } = "Delhi";  
  8.         public static void Main(string[] args)  
  9.         {  
  10.             Studentdetails Student1 = new Studentdetails();  
  11.             Console.WriteLine(nameof(Student1));  
  12.             Console.WriteLine(" Name= \{Student1.Name}");  
  13.             Console.WriteLine(" Address= \{Student1.Address} ");  
  14.   
  15.             Studentdetails Student2 = new Studentdetails  
  16.             { Name = "Vijay Verma", Address = "New Delhi" };  
  17.   
  18.             Console.WriteLine("\n");  
  19.             Console.WriteLine(nameof(Student2));  
  20.             Console.WriteLine(" Name= \{ Student2.Name}");  
  21.             Console.WriteLine(" Address= \{Student2.Address} ");  
  22.             Console.ReadKey();  
  23.         }  
  24.     }  
  25. }  

Output

VS15 output

Summary

In this article we learned how to initialize the Auto Property Initializer without using a constructor and can be initialized where they are declared. I hope you liked this feature of C# 6.0. Don't forget to read my other articles on the series " A new feature of C# 6.0". Share your opinion about this feature and how will you use it in your project? Your comments are most welcome. Happy Coding!