0

For the past few years I have been assigning a C# Property to a field.

I saw some code today that just had

    public String Title
    {
        get;set; 

    }

I was thinking this was just un-finished code. However the program worked and returned the Title String!

I have been doing this

    private string _title;

    public String Title
    {
        get { return this._title; }
        set { this._title = value; }

    }

So here my question is, is there any benefit, or need to write the properties by attaching them to a field like I have been doing for the past 2 years?

IEnumerable
  • 3,610
  • 14
  • 49
  • 78
  • 4
    You need to research [auto-implemented](http://msdn.microsoft.com/en-us/library/bb384054.aspx) properties. (Asking about the benefits is too broad a question for SO.) – Andy G May 24 '14 at 12:39
  • This is fantastic! `public string Address { get; private set; }` - I had no idea (time saver) – IEnumerable May 24 '14 at 12:42
  • This is called 'auto-implemented' properties, and has been available since C# 3.0. The only possible deficiency I can see of an auto-implemented property is that is cannot be passed as a `ref` parameter. – ach May 24 '14 at 12:43

0 Answers0