C# , Difference between property with variable and without variable

Kuntady Nithesh picture Kuntady Nithesh · Sep 6, 2011 · Viewed 7.7k times · Source

Possible Duplicate:
What's the difference between encapsulating a private member as a property and defining a property without a private member?

I know the basic functionality of properties . But as i go through documentation in depth i see they are declared just with get set and without a variables .

what is the diffeence between these two

public int EmpCode
{
    get { return _strEmpCode; }
    set { _strEmpCode = value; }
}  

and

public int EmpCode
{
    get; 
    set; 
}  

Is it just a easier way of writing which got as .net frameworks got upgraded . Or is there any functional difference ?

Answer

Paul picture Paul · Sep 6, 2011

The later is called an Automatic Property and is the same.They were introduced in C#3, you can read more about them here: http://trashvin.blogspot.com/2008/05/automatic-properties-and-object.html

Simply put, Automatic Properties are syntactic sugar so the developer has to type less code and the compiler will generate the private field and the public setter and getter for you.