HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpCritical

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

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
syntaxsetthewhatget

Problem

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; }?

Solution

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

Code Snippets

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

Context

Stack Overflow Q#5096926, score: 713

Revisions (0)

No revisions yet.