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 Nov 30, 2015, 10:46 PM EST
Thanks Humayun Kabir Mamun
Humayun Kabir MamunPosted Nov 30, 2015, 10:28 PM EST
Nice...
Banketeshvar NarayanPosted Nov 30, 2015, 6:33 AM EST
Thanks Suman Verma
Suman VermaPosted Nov 30, 2015, 12:49 AM EST
nice one
Banketeshvar NarayanPosted Nov 30, 2015, 12:41 AM EST
Thanks Yaduveer Saini
Yaduveer SainiPosted Nov 29, 2015, 11:19 PM EST
Nice Article Banketeshvar Narayan
Banketeshvar NarayanPosted Nov 29, 2015, 9:22 PM EST
Thanks Aishwarya
Aishwarya DPosted Nov 29, 2015, 8:47 PM EST
helpful
Banketeshvar NarayanPosted Nov 29, 2015, 8:23 PM EST
Thanks Pawan Chand for giving your precious time to read this article
Pawan ChandPosted Nov 29, 2015, 8:21 PM EST
Thank You!
Banketeshvar NarayanPosted Nov 29, 2015, 6:50 PM EST
Thanks Santhakumar Munuswamy
Santhakumar MunuswamyPosted Nov 29, 2015, 6:47 PM EST
Good one
Banketeshvar NarayanPosted Nov 29, 2015, 6:15 PM EST
Thanks Ajay Gandhi
Ajay GandhiPosted Nov 29, 2015, 6:12 PM EST
Helpful
Banketeshvar NarayanPosted Nov 29, 2015, 5:54 PM EST
Thanks Raja T
Raja TPosted Nov 29, 2015, 5:50 PM EST
Nice , thanks for sharing
Banketeshvar NarayanPosted Nov 29, 2015, 12:29 AM EST
Thanks Ankur Mistry
Ankur MistryTOP 100Posted Nov 28, 2015, 11:01 PM EST
Good. Thanks for share