Automatically Implemented Properties Feature in C# 3.0
Introduction of Automatic Properties in C# 3.0 make property declaration more concise. It saves some of your time in typing a lot of code.
Example:
class Employee
{
public string FirstName { get; set; }
}
Benefit of using Automatic Properties
We have been creating properties in so many projects and notice that we are writing so many lines of code just for creating a simple property.
Example: Employee class that does not use Automatic Properties
public class Employee {
private int iEmpId;
private string strFirstName;
private string strLastName;
public int ID{
get{
return iEmpId;
}
}
public string FirstName {
get {
return strFirstName;
}
set {
strFirstName= value;
}
}
public string LastName {
get {
return strLastName;
}
set {
strLastName = value;
}
}
}
In order to create a simple property, we are coding extra four or five lines code. So, if we have 100 properties in different entity classes, we have to type too much of code just for creating properties.
If you convert above Employee class with Automatic properties it look like
class Employee
{
public int ID{ get; private set; } // read-only
public string FirstName { get; set; }
public int LastName { get; set; }
}
Note: This is my first article to explain the C# 3.0 tutorial series.
Conclusion
It is one of C# 3.0 Language feature and nothing new except it helps you to improve your productivity.
More from kalyan
- Object and Collection Initializers Feature in C# 3.0
- WCF Features in .NET Framework 4.0
- Extension Methods Feature in C# 3.0
- Singleton Design Pattern in C#
- Lambda Expressions in C# 3.0
kalyan Recommends
Related Posts:
2 comments2 Comments so far
Leave a reply
[...] 7. Auto-Implemented Properties. [...]
Automatically Implemented Properties Feature in C# 3.0…
You’ve been kicked (a good thing) – Trackback from DotNetKicks.com…