C# 3.0 Features: Automatic Property (Part 2)

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

publicclassCustomer

{

public Customer() { }

//Conventional Way

privateint _XYZ;

publicint CustID2

    {

get { return _XYZ; }

set { _XYZ = value ; }

    }

//Automatic Property

publicint 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!!!