53

I see this quiet often in C# documentation. But what does it do?

public class Car
{
   public Name { get; set; }
}
Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789
naim5am
  • 1,334
  • 3
  • 17
  • 33
  • 20
    Can I ask why this has been marked as a duplicate? This was asked in 2009 and the "original" was asked in 2011! – Nathangrad May 12 '17 at 12:17

6 Answers6

52

It is shorthand for:

private string _name;

public string Name
{
    get { return _name; }
    set { _name = value; }
}

The compiler generates the member variable. This is called an automatic property.

Bryan Watts
  • 44,911
  • 16
  • 83
  • 88
  • 10
    ... although the name of the field is actually an "unspeakable" name (e.g. `<>__name`) which is invalid as a C# identifier. This stops you from directly accessing the field from your code. The specification term is "automatically implemented property" but most people do indeed call it an automatic property :) – Jon Skeet Aug 21 '09 at 06:19
  • 6
    ... But _why_? Why is this _any_ different than `public string Name;`? – Qix - MONICA WAS MISTREATED May 03 '17 at 12:27
  • @Qix that is the syntax for a field. Properties and fields are semantically distinct; there are reasons to use one or another in various situations. – Bryan Watts May 03 '17 at 15:51
  • @Bryan, if those reasons and situations build the context to use automatic properties, they should be explained. Unfortunately, that explanation is lacking in all answers here and in other questions. – user1840267 Aug 15 '17 at 09:59
  • @user1840267: The semantics you describe are of `properties`, a wider category than `automatic properties`, and out of scope for a question asking what they are, not how to use them. – Bryan Watts Aug 15 '17 at 21:25
  • 1
    @Brian: Ok, sorry. My point is the one Qix asked: After reading all the answers and comments, I still don't understand the advantage of the automatic property syntax vs. a simple declaration. I was hoping that some reasons and explanations would clarify that. – user1840267 Oct 12 '17 at 11:40
  • @user1840267: Less code, less potential for human error, less how and more why. Essentially the same reasons as other syntactic sugar. – Bryan Watts Oct 12 '17 at 15:29
36

In simple terms they are referred as property accessors. Their implementation can be explained as below

1.get{ return name} The code block in the get accessor is executed when the property is Read.

2.set{name = value} The code block in the set accessor is executed when the property is Assigned a new value.

Eg.(Assuming you are using C#)

 class Person
 {
     private string name;  // the name field
     public string Name    // the Name property
     {
         get
         {
             return name;
         }
         set
         {
             name = value;
         }
     }
 }
  1. Now when you refer to this property as below

    Person p = new Person();// Instantiating the class or creating object 'p' of class 'Person'

    System.Console.Write(p.Name);  //The get accessor is invoked here
    

The get accessor is invoked to Read the value of property i.e the compiler tries to read the value of string 'name'.

2.When you Assign a value(using an argument) to the 'Name' property as below

Person p = new Person();
p.Name = "Stack"  // the set accessor is invoked here
Console.Writeline(p.Name) //invokes the get accessor
Console.ReadKey(); //Holds the output until a key is pressed

The set accessor Assigns the value 'Stack" to the 'Name property i.e 'Stack' is stored in the string 'name'.

Ouput:

Stack

  • 10
    What's the reason for having a get and set in the first place, why not just use one accessor for both setting and getting, this is the question nobody seems capable of answering in simple terms. – JsonStatham Nov 13 '15 at 17:37
  • @JsonStatham it's so you can add code to check or modify the value before assignment. for example `set { name = value + "."; }` would a a dot to the string. following that same idea, `get` can modify the value before returning it. – UpTide Nov 21 '18 at 22:13
  • Just tell me the proper syntax and I am good. If you make "Name" static but with getter and setter, what is the syntax to access it, from the same class and from a different class? – Baruch Atta Apr 29 '19 at 12:55
10

It's an automatic read-write property. It's a C# 3.0 addition. Something like:

public class Car {
    private string name;
    public string Name { get { return name; } set { name = value; } }
}

except that you can't directly access the backing field.

Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789
4

It's called an Auto-Implemented Property and is new to C# 3.0. It's a cleaner syntax when your access to the property doesn't need any special behavior or validation. It's similar in function to:

public class Car
{
  private string _name;
  public string Name
  {
    get { return _name; }
    set {_name = value; }
  }
}

So it saves a fair amount of code, but leaves you the option later to modify the accessor logic if behavior or rules need to change.

Jeff Donnici
  • 2,738
  • 19
  • 17
3

It is the equivilent of doing:

private string _Text;

public string Text 
{
    get { return _Text; }
    set { _Text = value; }
}

Except you don't have access to the private variable while inside the class.

Darthg8r
  • 12,377
  • 15
  • 63
  • 100
  • 1
    So if you want to keep the private variable you have to use the "old" format (as in your example)? – Rado Mar 08 '15 at 18:18
1

Auto-Implemented Properties

SUMMARY:In C# 3.0 and later, auto-implemented properties make property-declaration more concise when no additional logic is required in the property accessors.

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186