Alex Rivera | Logout

c#: getter/setter

Asked 2011-07-15T15:03:35.870
147

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; }
Edit
Report

2 Answers

229

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; }
}
answered 2011-07-15T15:05:14.707
5

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

private string type;
public string Type
{
   get{ return type; }
   set{ type = value; }
}
answered 2011-07-15T15:06:07.663

Your Answer