Getter-Only & Auto Properties Initializers in C# 6

Getter-only auto properties

Before C# 6 it was mandatory to use setter with property but in C# 6 we can use it without setter.

In C# 5

  1. public class MonthlySalary_Earning  
  2.     {  
  3.         public string MonthName { get;}  
  4.         public double BasicSalary { get;}  
  5.         public double HRAExemption { get;}  
  6.         public double ConveyanceAllowance { get;}  
  7.         public double PersonalAllowance { get;}  
  8.         public double MedicalAllowance { get;}  
  9.         public double TelephoneBill { get;}  
  10.         public double Food_Bill { get;}  
  11.         public double OtherBills { get;}  
  12.         MonthlySalary_Earning()  
  13.         {  
  14.            MonthName = "Jan";  
  15.         }         
  16.     } 

The above code will not compile in C# 5 or lower versions and give errors like the following,

'PropertyInitialization.MonthlySalary_Earning.MonthName.get' must declare a body because it is not marked abstract or extern. Automatically implemented properties must define both get and set accessors.

And so on,

property

But in C# 5 we have an option that we can use access modifier private/protected setter with property.

E.g.

  1. public class MonthlySalary_Earning  
  2.     {  
  3.         public string MonthName             { getprivate set; }  
  4.         public double BasicSalary           { getprivate set; }  
  5.         public double HRAExemption          { getprivate set; }  
  6.         public double ConveyanceAllowance   { getprivate set; }  
  7.         public double PersonalAllowance     { getprivate set; }  
  8.         public double MedicalAllowance      { getprivate set; }  
  9.         public double TelephoneBill         { getprivate set; }  
  10.         public double Food_Bill             { getprivate set; }  
  11.         public double OtherBills            { getprivate set; }  
  12.   
  13.         MonthlySalary_Earning()  
  14.         {  
  15.            MonthName = "Jan";  
  16.         }         
  17.     }  

OR  

  1. public class MonthlySalary_Earning  
  2.     {  
  3.         public string MonthName             { getprotected set; }  
  4.         public double BasicSalary           { getprotected set; }  
  5.         public double HRAExemption          { getprotected set; }  
  6.         public double ConveyanceAllowance   { getprotected set; }  
  7.         public double PersonalAllowance     { getprotected set; }  
  8.         public double MedicalAllowance      { getprotected set; }  
  9.         public double TelephoneBill         { getprotected set; }  
  10.         public double Food_Bill             { getprotected set; }  
  11.         public double OtherBills            { getprotected set; }  
  12.   
  13.         MonthlySalary_Earning()  
  14.         {  
  15.            MonthName = "Jan";  
  16.         }         
  17.     } 

In C# 6

  1. public class MonthlySalary_Earning  
  2.     {  
  3.         public string MonthName             { get;}  
  4.         public double BasicSalary           { get;}  
  5.         public double HRAExemption          { get;}  
  6.         public double ConveyanceAllowance   { get;}  
  7.         public double PersonalAllowance     { get;}  
  8.         public double MedicalAllowance      { get;}  
  9.         public double TelephoneBill         { get;}  
  10.         public double Food_Bill             { get;}  
  11.         public double OtherBills            { get;}  
  12.   
  13.         public MonthlySalary_Earning()  
  14.         {  
  15.             MonthName = "Jan";  
  16.             BasicSalary = 50000.0;  
  17.             HRAExemption = 1000.0;  
  18.             ConveyanceAllowance = 1600.0;  
  19.            //set value for other properties also.  
  20.         }  
  21.   
  22.     }

Auto properties initializers

In C# 6 we can initialize the properties at the time of declaration

  1. public string MonthName             { get; } = "Jan";  
  2.        public double BasicSalary           { get; } = 50000.0;  
  3.        public double HRAExemption          { get; } = 1000.0;  
  4.        public double ConveyanceAllowance   { get; } = 1600.0;  
  5.        public double PersonalAllowance     { get; } = 50000.0;  
  6.        public double MedicalAllowance      { get; } = 5000.0;  
  7.        public double TelephoneBill         { get; } = 5000.0;  
  8.        public double Food_Bill             { get; } = 5000.0;  
  9.        public double OtherBills            { get; } = 5000;

Or

  1. public string MonthName             { getset;} = "Jan";  
  2.  public double BasicSalary           { getset;} = 50000.0;  
  3.  public double HRAExemption          { getset;} = 1000.0;  
  4.  public double ConveyanceAllowance   { getset;} = 1600.0;  
  5.  public double PersonalAllowance     { getset;} = 50000.0;  
  6.  public double MedicalAllowance      { getset;} = 5000.0;  
  7.  public double TelephoneBill         { getset;} = 5000.0;  
  8.  public double Food_Bill             { getset;} = 5000.0;  
  9.  public double OtherBills            { getset;} = 5000;

But in C# 5 and lower version it is not supported. Let me change the language from C# 6 to C# 5 and see the result.

If you are not aware how to change the C# language version in Visual Studio 2015 then check it here.

After changing the language from C#6 to C# 5. Now look at the following code window,

code window

It is clearly saying that the feature 'auto property initializer' is not available in C# 5. Please use language version 6 or greater.

Now I built this application and it throws the following build error.

built

Thus we can say that in C# 6, two useful features for properties has been added and they are Getter-only auto properties & Auto properties initializers.

In short we can say that in C# 6, the properties can be written as in the following:
  1. public       string    MonthName             { getset; }   
  2. //or  
  3. public      string     MonthName             { getprivate set; }   
  4. //or   
  5. public  string MonthName        { getprotected set; }   
  6. //or  
  7. public  string MonthName        { get; }   
  8. //or  
  9. public  string MonthName        { get; } = "Jan";  
  10. //or  
  11. public string MonthName         { getset;} = "Jan";  


Similar Articles