Thank you everyone for showing interest on my blog entry C# 3.0 Features: Automatic Property. Questions like, why this approach? What if there is any condition in property? Let me tell you, we have implemented the higher level of abstraction in C# 3.0. If you are simply creating a property (we often do) then the pain declaring the local variable and its value setting/getting are being taken care by the CLR. So internally CLR declares the property exactly like what we used to do earlier.
Let’s say
public class Customer
{
public Customer() { }
//Conventional Way
private int _XYZ;
public int CustID2
get { return _XYZ; }
set { _XYZ = value; }
}
//Automatic Property
public int CustID
get;
set;
For both the cases if you examine the assembly through IL DASM. You will get the following result (the red-boxed areas are for automatic property)
If you have noticed for the property CustID (which is Automatic) it created a private variable k__AutomaticallyGeneratedPropertyField0 of type int32. This is the extra work CLR does for us.
We do have backward compatibility with C# 3.0. That means all the code that you have written in previous version of C# will compile with no error (provided it is clean codeJ).
Namoskar!!!
Working from memory now, but if I remember correctly, these automatic properties will only work if you declare both get and set. ie, you can not have an automatic property with only get or only set. This is a major disappointment to me. I often create properties with only a get accessor. So I doubt that I will be able to use this feature very often.
Here is a reference to that effect:
http://community.bartdesmet.net/blogs/bart/archive/2007/03/03/c-3-0-automatic-properties-explained.aspx
I was about to write this entry in my blog which I found. Before that I got comment in my previous Blog
Hi,
There is a way to do that
Syntax is
private set;
Please visit http://blogs.msdn.com/wriju/archive/2007/03/27/c-3-0-features-automatic-property-part-3.aspx
Wriju
With this exciting release of .NET Framework 3.5 I have been exploring its magic and sharing the finding