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 { get; private set; }
  4. public double BasicSalary { get; private set; }
  5. public double HRAExemption { get; private set; }
  6. public double ConveyanceAllowance { get; private set; }
  7. public double PersonalAllowance { get; private set; }
  8. public double MedicalAllowance { get; private set; }
  9. public double TelephoneBill { get; private set; }
  10. public double Food_Bill { get; private set; }
  11. public double OtherBills { get; private set; }
  12. MonthlySalary_Earning()
  13. {
  14. MonthName = "Jan";
  15. }
  16. }

OR

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

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. public MonthlySalary_Earning()
  13. {
  14. MonthName = "Jan";
  15. BasicSalary = 50000.0;
  16. HRAExemption = 1000.0;
  17. ConveyanceAllowance = 1600.0;
  18. //set value for other properties also.
  19. }
  20. }

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 { get; set;} = "Jan";
  2. public double BasicSalary { get; set;} = 50000.0;
  3. public double HRAExemption { get; set;} = 1000.0;
  4. public double ConveyanceAllowance { get; set;} = 1600.0;
  5. public double PersonalAllowance { get; set;} = 50000.0;
  6. public double MedicalAllowance { get; set;} = 5000.0;
  7. public double TelephoneBill { get; set;} = 5000.0;
  8. public double Food_Bill { get; set;} = 5000.0;
  9. 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,

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 { get; set; }
  2. //or
  3. public string MonthName { get; private set; }
  4. //or
  5. public string MonthName { get; protected set; }
  6. //or
  7. public string MonthName { get; }
  8. //or
  9. public string MonthName { get; } = "Jan";
  10. //or
  11. public string MonthName { get; set;} = "Jan";