TechBubbles Microsoft Technology BLOG

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.

About the author

Kalyan Bandarupalli

My name is kalyan, I am a software architect and builds the applications using Microsoft .NET technologies. Here I am trying to share what I feel and what I think with whoever comes along wandering to Internet home of mine.I hope that this page and its contents will speak for me and that is the reason I am not going to say anything specially about my self here.

2 Comments

TechBubbles Microsoft Technology BLOG

Follow me

Archives

Tag Cloud