Properties in C#
Properties have been an important part of C#.They have evolved quite a bit since the initial version.They are a means to provide controlled access to the state of the objects.For example we can declare the state of a class using the following fields
public class Employee { public int Age; public string Name; public bool isPermanent; public string Address; }
But the problem with the above code is that the user of the class can assign any acceptable values for string and int data types.So for example Age can be assigned the value 1500 ,though it is a unrealistic values.So to provide controlled access to the state of the class properties were included in C#.
Using properties we can declare the above Employee class as
public class Employee { private int age; public int Age { get { return age; } set { if (age > 20 && age < 60) { age = value; } } } private string name; public string Name { get { return name; } set { name = value; } } private bool isPermanent; public bool IsPermanent { get { return isPermanent; } set { isPermanent = value; } } private string address ; public string Address { get { return address ; } set { address = value; } } }
Properties are used like variables but contain logic which is executed when value is fetched or assigned.This enables to verify the state of the class and keeps the implementation details hidden.
Auto Properties in C# 3.0
Though properties solved access problem but when creating properties we have to declare the backing field.In the above example IsPermanent and Address properties are just returning and setting the backing field.This is a redundant work which is required for every property declaration.In other words this is just a boiler-plate code which we had to write for the get and set accessors.
To avoid this declaration of the backing fields auto properties were introduced.
Using auto properties we can declare IsPermanent and Address properties as:
public bool isPermanent { get; set; } ; public string Address { get; set; } ;
So auto properties provide just a syntactic shortcut for declaring properties.
Auto Property Initializers in C# 6.0
Though auto properties simplifies declaration of properties but they have one limitation, they can not be initialized at the time of declaration.So the only way to initialized the auto properties is within constructors or any other method.
This can cause difficulty in understanding since the property is not initialized at the place of the declaration.
In C# 6 we can initialize the property at the place of the declaration.So the following is valid in C# 6.0:
public bool isPermanent { get; set; } = true; public string Address { get; set; } = GetDefaultAddress();
Above is a simple example of auto property initializers ,but they could be really useful in more complex scenarios.
For example if we consider a class having lots of properties.If we initialize such a property in the constructor then it can be difficult to understand the initialization logic for somebody looking at the code.
Below few auto properties are declared in a class
class Employee { public int Age { get; set; } public string CompanyName { get; set; } public string CompanyAddress { get; set; } public string Department { get; set} public decimal Bonus { get; set} public Employee() { this.CompanyName = "Name"; this.CompanyAddress = "XYZ"; this.Department = "Default Department"; this.Bonus = Constants.DefaultBonus; } }
We need to initialize auto properties in the constructor in the prior versions of C#.
By using the auto property initializer in C# 6.0 we can initialize them as:
class Employee { public int Age { get; set; } public string CompanyName { get; set; } = "Name"; public string CompanyAddress { get; set; }= "XYZ"; public string Department { get; set} = "Default Department"; public decimal Bonus { get; set}= Constants.DefaultBonus; }
Leave a Reply