142

I saw something like the following somewhere, and was wondering what it meant. I know they are getters and setters, but want to know why the string Type is defined like this. Thanks for helping me.

public string Type { get; set; }
Maya
  • 7,053
  • 11
  • 42
  • 53
  • http://weblogs.asp.net/dwahlin/archive/2007/12/04/c-3-0-features-automatic-properties.aspx – Snowbear Jul 15 '11 at 15:05
  • 4
    This is called an Auto-Property, have a look at this: http://msdn.microsoft.com/en-us/library/bb384054.aspx – Allov Jul 15 '11 at 15:05
  • This is the definition of a property named `"Type"`, the .NET type of which is `System.string`. There's nothing more to it. – Jon Jul 15 '11 at 15:05
  • 2
    I think that he might be confusing the naming of the Auto-Property with the Reflection class System.Type. http://msdn.microsoft.com/en-us/library/system.type.aspx – eandersson Jul 15 '11 at 15:21
  • http://stackoverflow.com/documentation/c%23/49/properties/3365/auto-implemented-properties#t=201610041453344442997 – Jon Schneider Oct 04 '16 at 14:55

9 Answers9

224

Those are Auto-Implemented Properties (Auto Properties for short).

The compiler will auto-generate the equivalent of the following simple implementation:

private string _type;

public string Type
{
    get { return _type; }
    set { _type = value; }
}
Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
  • 2
    @barlop as someone coming from google, with no clue, what needs to be updated? Is M. Hassans answer what you mean? thanks. –  Feb 07 '19 at 07:17
  • 1
    @Aethenosity in retrospect I think it's ok.. I was thinkinig in terms of examples of getter setter. The questioner has a valid case of getter setter that is far more succinct(as a one liner/ no second field necessary).. You can also write `public int b { get { return b * 2; } }` no second field necessary. But I think when you have the setter with a body then you need the second field. And this one shows a setter with a body. Though it does the same as the questioner's one liner. – barlop Feb 07 '19 at 14:38
  • 2
    @Aethenosity there is also c# 7 whereby sure you still need the second field when the setter has a body, but it has a `=>` syntax and no return keyword. Though that wasn't what I had in mind. I had in mind the second field being unnecessary, though I have since found that the second field is necessary sometimes. In the example here the second field isn't necessary('cos a default setter with no body would do this), but the answerer was putting it in(setter body and second field), to explain to the questioner what their line without the second field was doing. – barlop Feb 07 '19 at 14:39
42

That is an auto-property and it is the shorthand notation for this:

private string type;
public string Type
{
  get { return this.type; }
  set { this.type = value; }
}
Teoman Soygul
  • 25,584
  • 6
  • 69
  • 80
35

In C# 6:

It is now possible to declare the auto-properties just as a field:

public string FirstName { get; set; } = "Ropert";

Read-Only Auto-Properties

public string FirstName { get;} = "Ropert";
M.Hassan
  • 10,282
  • 5
  • 65
  • 84
20
public string Type { get; set; } 

is no different than doing

private string _Type;

public string Type
{    
  get { return _Type; }
  set { _Type = value; }
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Security Hound
  • 2,577
  • 3
  • 25
  • 42
8

This means that the compiler defines a backing field at runtime. This is the syntax for auto-implemented properties.

More Information: Auto-Implemented Properties

Pang
  • 9,564
  • 146
  • 81
  • 122
Maverik
  • 5,619
  • 35
  • 48
5

You can also use a lambda expression

public string Type
{
    get => _type;
    set => _type = value;
}
Frederic
  • 2,015
  • 4
  • 20
  • 37
Ahmad Aghazadeh
  • 16,571
  • 12
  • 101
  • 98
5

It's an automatically backed property, basically equivalent to:

private string type;
public string Type
{
   get{ return type; }
   set{ type = value; }
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Jamiec
  • 133,658
  • 13
  • 134
  • 193
4

These are called auto properties.

http://msdn.microsoft.com/en-us/library/bb384054.aspx

Functionally (and in terms of the compiled IL), they are the same as properties with backing fields.

Jeff
  • 35,755
  • 15
  • 108
  • 220
  • 1
    Can you still reference the private _type or type within the class, or you just use Type? – mikey Jul 15 '11 at 15:07
  • 2
    No, BUT you can specify the modifier for the auto property: public string Type { get; private set; } – Jeff Jul 15 '11 at 15:09
  • You would be unable to access _type in this case. – Security Hound Jul 15 '11 at 15:10
  • 1
    So in that case this.Type = "foo"; should be OK, but from outside instance.Type = "foo"; will not.. These auto props are definitely a helpful addition to the language. Thanks. – mikey Jul 15 '11 at 15:12
1

With the release of C# 6, you can now do something like this for private properties.

public constructor()
{
   myProp = "some value";
}

public string myProp { get; }
Pang
  • 9,564
  • 146
  • 81
  • 122
Anonymous
  • 1,978
  • 2
  • 28
  • 37
  • 3
    you can set initial value directly in C# 6: public string myProp { get; } ="some value") without constructor; – M.Hassan Jul 19 '16 at 08:39