Coder Perfect

In C#, what does the get; set; syntax mean?

Problem

I’m learning ASP.NET MVC, and while I can read English papers, I’m not sure what’s going on with this code:

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

What does it mean when you say “get; set;”?

Asked by kn3l

Solution #1

It’s an auto property, and it’s effectively a shorthand for the following (the compiler will generate comparable code):

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

Answered by Klaus Byskov Pedersen

Solution #2

As far as I can tell, get; set; is a “auto property,” which is shorthand for writing a property with a “backing field,” as @Klaus and @Brandon explained. As an example, in this case:

public class Genre
{
    private string name; // This is the backing field
    public string Name   // This is your property
    {
        get => name;
        set => name = value;
    }
}

However, if you’re like me, you don’t really comprehend what properties and accessors are, and you also don’t have the finest comprehension of some basic terminology. MSDN is a fantastic resource for learning things like this, but it’s not always intuitive for newcomers. So I’ll try to explain this in greater detail below.

Get and set are accessors, which means they can access data and information in private fields (typically from a backing field) and from public properties (as you can see in the above example).

Without a doubt, the above statement is perplexing, so let’s look at some examples. Let’s pretend this code is about music genres. So, within the class Genre, we’ll want a variety of musical genres. Let’s say we want three different genres: hip hop, rock, and country music. To do so, we’d utilise the Class’s name to create new instances of the class.

Genre g1 = new Genre(); //Here we're creating a new instance of the class "Genre"
                        //called g1. We'll create as many as we need (3)
Genre g2 = new Genre();
Genre g3 = new Genre();

//Note the () following new Genre. I believe that's essential since we're creating a
//new instance of a class (Like I said, I'm a beginner so I can't tell you exactly why
//it's there but I do know it's essential)

Now that we’ve established the Genre class instances, we can use the ‘Name’ attribute that we set up earlier to specify the genre names.

public string Name //Again, this is the 'Name' property
{ get; set; } //And this is the shorthand version the process we're doing right now 

By writing the following, we may change the name of ‘g1’ to Hip Hop.

g1.Name = "Hip Hop";

What’s going on here is a little complicated. As I previously stated, obtain and set access information from private fields that you would otherwise be unable to access. get can only read and return data from that private field. Only set has permission to write to that private field. We can achieve both of those functions by having a property with both get and set. Likewise, by writing g1. We’re explicitly using the set function from our Name field with Name = “Hip Hop.”

value is an implicit variable used by set. Basically, any time the word “value” appears in a set, it refers to a variable, the “value” variable. When we type g1.Name =, we’re passing in the value variable, which is “Hip Hop” in this example. As a result, you can think about it like this:

public class g1 //We've created an instance of the Genre Class called "g1"
{
    private string name;
    public string Name
    {
        get => name;
        set => name = "Hip Hop"; //instead of 'value', "Hip Hop" is written because 
                              //'value' in 'g1' was set to "Hip Hop" by previously
                              //writing 'g1.Name = "Hip Hop"'
    }
}

It’s worth noting that the sample above isn’t actually written in code. It’s more of a fictitious code that portrays what’s happening behind the scenes.

So, now that the Name of the g1 instance of Genre has been set, I suppose we can get the name by writing

console.WriteLine (g1.Name); //This uses the 'get' function from our 'Name' Property 
                             //and returns the field 'name' which we just set to
                             //"Hip Hop"

is that “Hip Hop” would appear on our console.

So, for the sake of this discussion, I’ll also include outputs in the example.

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

public class MainClass
{
    public static void Main()
    {
        Genre g1 = new Genre();
        Genre g2 = new Genre();
        Genre g3 = new Genre();

        g1.Name = "Hip Hop";
        g2.Name = "Rock";
        g3.Name = "Country";

        Console.WriteLine ("Genres: {0}, {1}, {2}", g1.Name, g2.Name, g3.Name);
    }
}

Output:

"Genres: Hip Hop, Rock, Country"

Answered by Josie Thompson

Solution #3

These are built-in features.

Basically another way of writing a property with a backing fie

public class Genre
{
    private string _name;

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

Answered by Brandon

Solution #4

This is the quickest way to do it:

public class Genre
{
    private string _name;

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

Answered by froeschli

Solution #5

It’s a shortcut for making data members public so you don’t have to establish private data members directly. For you, C# will build a private data member.

You could just make your data members public without utilizing this shortcut, but if you wanted to update the data member’s implementation to include logic, you’d have to break the interface. In a nutshell, it’s a shortcut for writing more flexible code.

Answered by Kelsey

Post is based on https://stackoverflow.com/questions/5096926/what-is-the-get-set-syntax-in-c