New Features in C# 6.0 – Auto-Property Initializers

Initialize property is repetitive task, and cannot be done in the same line as we can can done for fields. For example we can write:

public class Person
{
 private string m_Name="Default Name";
 public string Name {get;set;}
 public Person()
 {
   Name=m_Name;
 }
 
}

As we can see Property can be initialized only in the constructor, beside the filed which can be initialized in the same line where it is declared. The new feature in C# 6.0 defines Auto-Property initializer alowing property to be initialized like fields. The following code snippet shows the Auto-Property Initializer;

public class Person
{
 static string m_Name="Default Name";
 static string Name {get;set;}=m_Name;
}

Posted Oct 10 2014, 09:48 PM by Bahrudin
developers.de is a .Net Community Blog powered by daenet GmbH.