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?

3
  • 4
    You need to research auto-implemented properties. (Asking about the benefits is too broad a question for SO.) Commented May 24, 2014 at 12:39
  • This is fantastic! public string Address { get; private set; } - I had no idea (time saver) Commented May 24, 2014 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. Commented May 24, 2014 at 12:43

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.