Primary Constructor is Removed From C# 6.0


On November 12, 2014 Microsoft announced  the release of Visual Studio 2015 on the day of Visual Studio Connect() from New York, USA where they announced many new features as well as enhancements with C# 6.0. So we should understand that a Primary Constructor is not applicable with C# 6.0 but we can do the same thing using  Auto Implemented Properties Initializers.

Basically a Primary Constructor is a feature of C# that was announced with Visual Studio 2014  by which a class or structure can accept parameters in the class definition without a formal constructor declaration. But what we would like to say is that now it has been removed from Visual Studio 2015 Preview. So  in C# 6.0 it's not possible to use a primary constructor. So before learning more about it let's see it with an example program.

Example: In Visual Studio 2014.

  1. public class Square(double side)  
  2. {  
  3.     public double Side{get;set;}    
  4. }  
Note: The preceding program is written in Visual Studio 2014. That is fine but it gives a syntax error when we want to write this code in any other IDE except Visual Studio 2014. Let's do it with Visual Studio 2015 or any other IDE except Visual Studio 2014.

Figure: I

Primary Constructor with Visual Studio 2015.


Figure: II

With Visual Studio 2013.


Note: We saw that when we are writing the code of the Primary Constructor in Visual Studio 2015 as well as 2013, the same error is found in both IDEs. But the same line of code will run well in Visual Studio 2014. 

Usage of Primary Constructor

  • The main use of a primary constructor is to initialize the auto-implemented properties by the variable that are the parameter of the Primary Constructor.
  • A primary constructor behaves like a default constructor because it shows the minimal required parameters.


The following program is written in Visual Studio 2014:


  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  6. namespace PrimaryConstructor  
  7. {  
  8.    public class Squre(double side)  
  9.    {  
  10.       public double Side { getset; }=side;   
  12.       //Area  
  13.       public double AreaOfSquare()  
  14.       {  
  15.         return Convert.ToDouble(side*side);  
  16.       }  
  18.       static void Main(string[] args)  
  19.       {  
  20.          Square sq=new Square(15.8);  
  21.          sq.AreaOfSquare();  
  22.       }  
  23.    }  
  24. }  


Auto Implemented Properties Initializers

Now we can do the same work using auto implemented property Initializers that were announced by Microsoft on November 12, 2014 on the day of #vsconnect.

We can initialize the getter and setter properties together as well as getter only also.


Initializing the getter and setter together.


  1. public class Book  
  2. {  
  3.   public string Title { getset; }="Visual C# 6.0";  
  5.   public string Auther{get;}="Microsoft";   
  6. }  



Initializing the getter only. 

  1. public class Book  
  2. {  
  3.    public string Title { get; }="Visual C# 6.0";  
  5.    public string Auther{get;}="Microsoft";   
  6. }  

 Program Output 


In this article we learned about the Auto Implemented Properties Initializers and how it fullfills the requirements of a Primary Constructor.

Similar Articles