16

Why are we able to write

public int RetInt
{
   get;set;
}

instead of

public int RetInt
{
   get{return someInt;}set{someInt=value;}
}

What is the difference between the two?

lorddarkangel
  • 212
  • 3
  • 21
  • possible duplicate of [Correct use of C# properties](http://stackoverflow.com/questions/3532038/correct-use-of-c-sharp-properties) – nawfal Jun 03 '13 at 17:28

4 Answers4

25

This feature is called Auto implemented properties and introduced with C# 3.0

In C# 3.0 and later, auto-implemented properties make property-declaration more concise when no additional logic is required in the property accessors. They also enable client code to create objects. When you declare a property as shown in the following example, the compiler creates a private, anonymous backing field that can only be accessed through the property's get and set accessors.

class Customer
{
    // Auto-Impl Properties for trivial get and set 
    public double TotalPurchases { get; set; }
    public string Name { get; set; }
    public int CustomerID { get; set; }

For your question

What is the difference between the two?

In your case, none. Since you are not doing anything while setting or retrieving the value, but suppose you have want to do some validation or want to perform other types of check then :

private int someInt;
public int RetInt
{
    get
    {
        if (someInt > 0)
            return someInt;
        else
            return -1;
    }
    set { someInt = value; } // same kind of check /validation can be done here
}

The above can't be done with Auto implemented properties.

One other thing where you can see the difference is when initializing a custom class type property.

If you have list of MyClass Then in case of Normal property, its backing field can be initialized/instantiated other than the constructor.

private List<MyClass> list = new List<MyClass>();
public List<MyClass> List
{
    get { return list; }
    set { list = value; }
}

In case of Auto implemented property,

public List<MyClass> SomeOtherList { get; set; }

You can only initialize SomeOtherList in constructor, you can't do that at Field level.

Habib
  • 219,104
  • 29
  • 407
  • 436
  • This doesn't really answer the question. – Ash Burlaczenko Jan 28 '13 at 10:27
  • 8
    @AshBurlaczenko, they question is why this is allowed, REASON -> its Auto implemented property, The difference between two, I just added to my answer – Habib Jan 28 '13 at 10:33
  • I think it should be mentioned _why_ this is done. http://stackoverflow.com/questions/1180860/public-fields-versus-automatic-properties – mowwwalker Jan 28 '13 at 19:38
7

How are these two different ?

There are different at least by 2 points:

  1. In normal property you have to define a field before (someInt in your case)
  2. In normal property you can set a breakpoint into the set/get modifiers, instead in auto property can not do that.

In other words: if you need "just property", use auto-properties, if you need more control over workflow (raise an event on set, debug, run other stuff inside), use "normal" properties.

Tigran
  • 61,654
  • 8
  • 86
  • 123
2

These are auto implemented properties. Please see http://msdn.microsoft.com/en-us/library/bb384054.aspx for more info.

Basic reason why these were introduced was to reduce the overhead of programmer of creating a private variable like someInt which had little use than being used in a property.

mihirj
  • 1,199
  • 9
  • 15
2

Actually these aren't really different, in both cases you have a private field that corresponds to your property, but in the first case it is generated by the compiler and hidden.

If you need to use the variable behind the property quite often in your class, I think it's better to declare your property the old way (2nd one), because each time you will access it this will call the getter if you do it the "new" way.

If you only need it to be used from outside your class (or in most of cases), then you can go with the "new" way (1st one)

ppetrov
  • 3,077
  • 2
  • 15
  • 27
  • Is there a way to access the "behind" property when using the "new"/auto-implemented fields (I've tried using _varName for accessing public VarName {get; set;} since that seems to be Microsoft's convention, however _varName is not recognized by the compiler). Is there a token or something that can be used that I'm missing ? Thanks. – samus Jan 28 '13 at 15:28
  • No, there is no way to directly access the autogenerated field. When the property is not virtual, it would be legal for the compiler to write code that directly accesses the field when used in the same class, but the current compiler does not do so. Do note however, that the JIT is extremly likely to inline the varname_get method, which makes using it no slower than directly using the field. (Again, unless the auto-property is virtual). – Kevin Cathcart Jan 28 '13 at 16:52