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

I’m learning ASP.NET MVC and came across this code in C#:

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

I don’t fully understand what { get; set; } does. Can someone explain how C# get set works and what it means in this context?

Hey! I remember being confused by this too when I first started with C# :sweat_smile:.

The { get; set; } syntax is called an auto-implemented property. In your example:

public string Name { get; set; }
get allows you to read the value (var n = genre.Name;).
set allows you to assign a value (genre.Name = "Rock";).

C# automatically creates a hidden backing field behind the scenes, so you don’t have to write extra code to store the value.

I’ve used { get; set; } in almost every class I’ve written in ASP.NET MVC. Think of it as a shortcut for this:

private string _name;

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

The auto-property { get; set; } just does all of this for you automatically. It’s neat because it keeps your code clean and concise while still letting you read and write the property.

Another way I explain it when mentoring others:

get → “Hey, can I look at this value?”

set → “Hey, can I change this value?”

So in your Genre class, Name is just a property that can be read and modified from outside the class.

Later on, if you want, you can even make it read-only or add logic inside the getter/setter. For example:

public string Name { get; private set; } // can only be set from inside the class

This gives you flexibility while keeping your code clean.

I often start with auto-properties { get; set; } for simplicity, and only add custom logic to the getters or setters when I need validation or extra behavior. Keeps classes simple at first and easy to maintain.