New Features in C# 6.0 – Primary Constructors


Update: This feature is removed from the C# 6.0 specification, probably for the next version of C#.
Primary constructors reduced declaration of various constructors with different arguments. Primary constructors is declare on the type declaration and this is why it so special. For example:


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

}

We can define Primary Constructor in combination on Auto-Property Initializer on the following way:

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

}
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s