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
- public class MonthlySalary_Earning
- {
- public string MonthName { get;}
- public double BasicSalary { get;}
- public double HRAExemption { get;}
- public double ConveyanceAllowance { get;}
- public double PersonalAllowance { get;}
- public double MedicalAllowance { get;}
- public double TelephoneBill { get;}
- public double Food_Bill { get;}
- public double OtherBills { get;}
- MonthlySalary_Earning()
- {
- MonthName = "Jan";
- }
- }
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,

But in C# 5 we have an option that we can use access modifier private/protected setter with property.
E.g.
- public class MonthlySalary_Earning
- {
- public string MonthName { get; private set; }
- public double BasicSalary { get; private set; }
- public double HRAExemption { get; private set; }
- public double ConveyanceAllowance { get; private set; }
- public double PersonalAllowance { get; private set; }
- public double MedicalAllowance { get; private set; }
- public double TelephoneBill { get; private set; }
- public double Food_Bill { get; private set; }
- public double OtherBills { get; private set; }
- MonthlySalary_Earning()
- {
- MonthName = "Jan";
- }
- }
OR
- public class MonthlySalary_Earning
- {
- public string MonthName { get; protected set; }
- public double BasicSalary { get; protected set; }
- public double HRAExemption { get; protected set; }
- public double ConveyanceAllowance { get; protected set; }
- public double PersonalAllowance { get; protected set; }
- public double MedicalAllowance { get; protected set; }
- public double TelephoneBill { get; protected set; }
- public double Food_Bill { get; protected set; }
- public double OtherBills { get; protected set; }
- MonthlySalary_Earning()
- {
- MonthName = "Jan";
- }
- }
In C# 6
- public class MonthlySalary_Earning
- {
- public string MonthName { get;}
- public double BasicSalary { get;}
- public double HRAExemption { get;}
- public double ConveyanceAllowance { get;}
- public double PersonalAllowance { get;}
- public double MedicalAllowance { get;}
- public double TelephoneBill { get;}
- public double Food_Bill { get;}
- public double OtherBills { get;}
- public MonthlySalary_Earning()
- {
- MonthName = "Jan";
- BasicSalary = 50000.0;
- HRAExemption = 1000.0;
- ConveyanceAllowance = 1600.0;
- //set value for other properties also.
- }
- }
Auto properties initializers
In C# 6 we can initialize the properties at the time of declaration
- public string MonthName { get; } = "Jan";
- public double BasicSalary { get; } = 50000.0;
- public double HRAExemption { get; } = 1000.0;
- public double ConveyanceAllowance { get; } = 1600.0;
- public double PersonalAllowance { get; } = 50000.0;
- public double MedicalAllowance { get; } = 5000.0;
- public double TelephoneBill { get; } = 5000.0;
- public double Food_Bill { get; } = 5000.0;
- public double OtherBills { get; } = 5000;
Or
- public string MonthName { get; set;} = "Jan";
- public double BasicSalary { get; set;} = 50000.0;
- public double HRAExemption { get; set;} = 1000.0;
- public double ConveyanceAllowance { get; set;} = 1600.0;
- public double PersonalAllowance { get; set;} = 50000.0;
- public double MedicalAllowance { get; set;} = 5000.0;
- public double TelephoneBill { get; set;} = 5000.0;
- public double Food_Bill { get; set;} = 5000.0;
- public double OtherBills { get; set;} = 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.
After changing the language from C#6 to C# 5. Now look at the following 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.

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:
- public string MonthName { get; set; }
- //or
- public string MonthName { get; private set; }
- //or
- public string MonthName { get; protected set; }
- //or
- public string MonthName { get; }
- //or
- public string MonthName { get; } = "Jan";
- //or
- public string MonthName { get; set;} = "Jan";

Banketeshvar NarayanPosted Dec 1, 2015, 3:46 AM
Thanks Humayun Kabir Mamun
Humayun Kabir MamunPosted Dec 1, 2015, 3:28 AM
Nice...
Banketeshvar NarayanPosted Nov 30, 2015, 11:33 AM
Thanks Suman Verma
Suman VermaPosted Nov 30, 2015, 5:49 AM
nice one
Banketeshvar NarayanPosted Nov 30, 2015, 5:41 AM
Thanks Yaduveer Saini
Yaduveer SainiPosted Nov 30, 2015, 4:19 AM
Nice Article Banketeshvar Narayan
Banketeshvar NarayanPosted Nov 30, 2015, 2:22 AM
Thanks Aishwarya
Aishwarya DPosted Nov 30, 2015, 1:47 AM
helpful
Banketeshvar NarayanPosted Nov 30, 2015, 1:23 AM
Thanks Pawan Chand for giving your precious time to read this article
Pawan ChandPosted Nov 30, 2015, 1:21 AM
Thank You!
Banketeshvar NarayanPosted Nov 29, 2015, 11:50 PM
Thanks Santhakumar Munuswamy
Santhakumar MunuswamyPosted Nov 29, 2015, 11:47 PM
Good one
Banketeshvar NarayanPosted Nov 29, 2015, 11:15 PM
Thanks Ajay Gandhi
Ajay GandhiPosted Nov 29, 2015, 11:12 PM
Helpful
Banketeshvar NarayanPosted Nov 29, 2015, 10:54 PM
Thanks Raja T
Raja TPosted Nov 29, 2015, 10:50 PM
Nice , thanks for sharing
Banketeshvar NarayanPosted Nov 29, 2015, 5:29 AM
Thanks Ankur Mistry
Ankur MistryPosted Nov 29, 2015, 4:01 AM
Good. Thanks for share