TechBubbles

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.

Related Posts:

2 comments

2 Comments so far

  1. Tech Bubbles » C# 3.0 features July 12th, 2008 7:14 am

    [...] 7. Auto-Implemented Properties. [...]

  2. DotNetKicks.com July 15th, 2008 1:36 pm

    Automatically Implemented Properties Feature in C# 3.0…

    You’ve been kicked (a good thing) – Trackback from DotNetKicks.com…

Leave a reply