Sunday, January 4, 2015

Primary Constructor in C# 6.0

 

In my previous post I explain about Auto Property Initializer in C# 6.0. Now lets see another new feature in C# 6.0 which is Primary constructor.

First we will see the following code.

  1. public class Country
  2. {
  3.     public string Name { get; private set; }
  4.  
  5.     public Country(string name)
  6.         {
  7.         Name = name;
  8.         }
  9.  
  10. }

here we have a class which has a property with a private setter and assigning the value for the name property inside the constructor.

Now in C# 6.0 you can write the above code in a more simplified manner as follows.

  1. public class Country(string name)
  2.     {
  3.         public string Name { get;}=name;
  4.  
  5.  
  6.     }

This is what is called primary constructor in C# 6.0 . Notice the parameters added just right of the class definition. This tells the compiler to add a constructor to the Country class with one parameters: name. So we don’t need to create separate constructor with parameters and then assign the values to the property , compiler do all those for us.

For one class only one primary constructor is allowed. If you want to have constructors with some more parameters you can create new constructor and also call the primary constructor inside it. See the following example.

  1. public class Country(string name)
  2.     {
  3.         public string Name { get; } = name;
  4.     public List<string> Regions { get; private set; }
  5.     public Country(List<string> regions, string name):this(name)
  6.     {
  7.         Regions = regions;
  8.     }
  9. }

Here when you call the constructor with two parameters it will assign the value to the property “Regions” and it has call the primary constructor it will set the value to the property “Name”. Hope you understand the concept behind this.

Hope this is helpful.

Happy Coding !!!!!!

Friday, January 2, 2015

Auto Property Initializer

 

Anyone who have worked with properties in a class must have experienced that it was little annoying that we have to have private fields and assigned if we want to have default values for properties.

See the following example which we used to do when we have collection property.

  1. public class Country
  2.     {
  3.         private List<string> _regions = new List<string>();
  4.         public List<string> Regions { get { return _regions} set { value = _regions; } }
  5.  
  6.     }

but this is no more needed. We can simply write it as below.

  1. public class Country
  2.     {
  3.         public List<string> Regions { get; set; }=new List<string>();
  4.  
  5.     }

Also this is useful when you want to assign default values for read only fields.

Before C# 6.0

  1. public class Country
  2.   {
  3.       public string Name { get; }
  4.       public Country()
  5.       {
  6.           this.Name = "Sri Lanka";
  7.       }
  8.  
  9.   }

In C# 6.0

  1. public class Country
  2.   {
  3.       public string Name { get; }="Sri Lanka";
  4.  
  5.   }

 

This is syntactic sugar added in c# 6.0. hope this is helpful.

 

Happy Coding !!!!!!