C# 3.0 Features: Automatic Property

C# 3.0 Features: Automatic Property

Rate This
  • Comments 15

I am super excited with the Orcas March 2007 CTP release. I was doing the hands on with the C# and LINQ and as you know C# 3.0 comes with a lot of new things which increases the level of abstraction for the developer. We now write less and do more. As I often have mentioned in my previous entries and presentations with Microsoft Partner that now have think more on why not on how. I remember the day when I first learned the Property. I used to scratch my head on what to do with get or set. I probably started it now as we have snippet for property. Anyways in conventional way we declare property in a class like

 

public class Customer

{

    public Customer() { }

 

    private int _CustID;

 

    public int CustID

    {

        get { return _CustID; }

        set { _CustID = value; }

    }    

           

}

 

Now C# 3.0 suggests us not to invest so much. Rather than using property like

 

private int _CustID;

 

public int CustID

{

    get { return _CustID; }

    set { _CustID = value; }

}   

 

We can declare property like

 

public int CustID

{

    get;

    set;

}

 

No more private variable or = or value etc required. This is Automatic property in C# 3.0.

 

 

Namoskar!!!

Leave a Comment
  • Please add 6 and 6 and type the answer here:
  • Post
  • PingBack from http://paanis.wordpress.com/2007/03/24/c-30-new-features/

  • But what are the benefits then, compared with a public variable (which one shouldn't do, sure)?

    The benefits of a property was to me to do MORE than just assigning a variable or returning a variables value.

    This new feature seems to force just the opposite?!?

  • Just like Uwe I wonder if can later on change the storage of the data. What if my property later on changes to be the computation of some other values or what if setting that property should also change other parts of that class?

    On a first look this feature seems to make properties easy in the first place but harder for changing the class later on.

  • La nueva versión de C# trae una característica que se conoce como propiedades automáticas, que no es

  • Two places that you may need a property and not a field are when you implement an interface that defines a property and when you make a dll for others to use where it is not convienient to change from a field to a property later.

  • Hello everyone,

    Automatic property is just another way of declaring it. You can use the good old property declaration style if your property contains complex calculation and trick.  For C# 3.0 what this kind of property declaration does is that CLR interns defines a property with the syntax something like

    private int _XYZ;

    public int CustID

    {

       get { return _XYZ; }

       set { _XYZ = value; }

    }

    C# 3.0 enables us to increase the level of abstraction. I will come up with the answer in more details in next entry.

    Wriju

  • I would guess that this gives you the benefit of using properties without the overhead of the code. Then, as soon as you need more sophisticated actions within the property or class, you can expand the class with an explicit get & set.

    Is that right?

  • Hi Szurgot,

    Thanks for the feedback. Yes you are correct. This is just another way of declaring Property.

    Wriju

  • With this exciting release of .NET Framework 3.5 I have been exploring its magic and sharing the finding

  • link http://susanneguitierr.mysite.com/ page http www

  • website http://latoniawarford.00me.com/ website home domain

  • page http://genevaokelly.jokestan.cc/ domain domain website

  • What is valium used for. Valium statistics. Valium picture. Valium.

Page 1 of 1 (15 items)