What is the { get; set; } syntax in C#?

kn3l picture kn3l · Feb 23, 2011 · Viewed 1.2M times · Source

I am learning ASP.NET MVC and I can read English documents, but I don't really understand what is happening in this code:

public class Genre
{
    public string Name { get; set; }
}

What does this mean: { get; set; }?

Answer

Klaus Byskov Pedersen picture Klaus Byskov Pedersen · Feb 23, 2011

It's a so-called auto property, and is essentially a shorthand for the following (similar code will be generated by the compiler):

private string name;
public string Name
{
    get
    {
        return this.name;
    }
    set
    {
        this.name = value;
    }
}