The first C# 6.0 feature which we are going to see today is Auto Property Initializer, with the previous versions normally we declare and initialize auto properties separately with a must constructor to create objects. But with the latest version we can Auto initialize the property without any explicit constructor declaration. Hearing it quite new, yes this feature is introduced to avoid having an object to initialize the property, rather do it while declaration itself. Let’s see how we can do that programmatically. First lets see a quick look on how we initialize properties with the first few versions of C#.Net
Later with the upcoming versions we still have some enhanced feature where we put the set and get without separate initializers but still we should require a Constructor to initialize the property and its mandatory as shown below. Visual Studio have a short cut keys to create properties if you are not aware of, just type “prop” and press the tab button twice which create a default property code which you can customize it as per the requirement. This will be handy when creating a multiple properties for the class object.
With the newer version C# 6.0 its further enhanced and now we can set the property initializers while declaring itself without having a constructor to implement with the values. So in simple we can term now properties as well like variable fields where we can assign the property with the values while declaration itself without any separate code inside the constructor as shown below. This Auto Property initializer will be more useful for data properties which will carry data with out any specific methods or function implementation.