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