C# 3.0 Features: Automatic Property (Part 2)

C# 3.0 Features: Automatic Property (Part 2)

Rate This
  • Comments 4

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

Leave a Comment
  • Please add 4 and 7 and type the answer here:
  • Post
Page 1 of 1 (4 items)