What is the syntax in {get; group;} C?

I am learning ASP.NET MVC and can read English documents, but I don't know what happened in this Code:

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

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

#1 building

Such {get; set;} {get; set;} {get; set;} syntax is called automatic property, C # 3.0 syntax

You must compile using Visual C 2008 / csc v3.5 or later. However, you can compile output as low as. NET Framework 2.0 (classes that do not need to run or support this feature).

#2 building

Get settings are access modifiers to properties. Gets the read property field. Set sets the property value. Getting is like read-only access. Sets are like write only access. To use a property as read-write, you must use both get and set.

#3 building

As far as I know, {get; set;} {get; set;} {get; set;} is an "automatic attribute", just like @ Klaus and @ Brandon said, it is a shorthand for the attribute with "backing field". Therefore, 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;
    }
}

But if you're like me (about an hour or so), you don't really know what properties and visitors are, and you don't have the best understanding of some basic terms. MSDN is a great tool for learning such knowledge, but it is not always easy for beginners to understand. So I'll try to explain this in more depth here.

get and set are accessors, which means they can access data and information in private fields (usually from backing fields), and usually from public properties (as shown in the example above).

Admittedly, the above statement is confusing, so let's look at some examples. Suppose this code refers to the music genre. Therefore, in genre categories, we will need different genres of music. Suppose we want to have three types: hip hop, rock and country. To do this, we will use the name of the class to create a new instance 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 have created an instance of the Genre class, we can use the 'Name' property set above to set the type Name.

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

We can set the name "g1" to hip-hop by writing the following command

g1.Name = "Hip Hop";

What happened here is a little complicated. As I said before, get and set access to information from a private field, otherwise you won't be able to access it. Get can only read information from the private field and return it. Set can only write information in this private field. However, by having the properties of get and set at the same time, we can achieve these two functions. By writing g1.Name = "Hip Hop"; we use the set function in the Name attribute

Set uses an implicit variable called value. Basically, this means that as long as you see "value" in set, it means variable. Value variable. When we write g1.Name = we use = to pass the value variable, in this case, "Hip Hop.". Therefore, you can basically think as follows:

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 is important to note that the above example is not actually written in code. More hypothetical code, which represents what happens in the background.

So now that we have set the name of Genre's g1 instance, I believe 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"

If you run this command, you will see "Hip Hop" in the console.

So, for explanation purposes, I'll also use the output to complete 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"

#4 building

Basically, this is a shortcut:

class Genre{
    private string genre;
    public string getGenre() {
        return this.genre;
    }
    public void setGenre(string theGenre) {
        this.genre = theGenre;
    }
}
//In Main method
genre g1 = new Genre();
g1.setGenre("Female");
g1.getGenre(); //Female

#5 building

In Visual Studio, if you define property X in a class and only want to use that class as a type, after you build the project, you receive a warning that field X has never been assigned to it and will always have its default value.

You will not receive this warning by adding {get; set;} {get; set;} as the X property.

In addition, in Visual Studio 2013 and later, you can see all references to this property by adding {get; set;} {get; set;}.

Tags: Attribute

Posted on Sat, 11 Jan 2020 00:15:27 -0500 by mikewooten