2023-06-02 09:00:01

{get;设置;}语法在c# ?

我正在学习ASP。NET MVC和我可以阅读英文文档,但我真的不明白这段代码中发生了什么:

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

这意味着什么:{get;设置;} ?


当前回答

基本上,它是一个快捷方式:

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

其他回答

当属性出现在右侧(RHS)时调用Get。 属性出现在左侧(LHS)时调用 的'='符号

对于自动实现的属性,后台字段在后台工作,不可见。

例子:

public string Log { get; set; }

而对于非自动实现的属性,支持字段是前面的,作为私有作用域变量可见。

例子:

private string log;

public string Log
{
    get => log;
    set => log = value;
}

另外,这里值得注意的是getter和setter可以使用不同的“支持字段”

所以在我看来{get;设置;}是一个“auto属性”,就像@Klaus和@Brandon说的是写一个带有“支持字段”的属性的速记。在这种情况下:

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

然而,如果你像我一样——大约一个小时前——你并不真正理解什么是属性和访问器,你也没有最好的理解一些基本的术语。MSDN是学习这些东西的一个很好的工具,但对于初学者来说并不总是容易理解。我将在这里更深入地解释这个问题。

Get和set是访问器,这意味着它们能够访问私有字段(通常从支持字段)中的数据和信息,并且通常从公共属性中这样做(正如您在上面的示例中看到的那样)。

不可否认,上面的说法是相当令人困惑的,所以让我们来看看一些例子。假设这段代码指的是音乐类型。在Genre类中,我们需要不同类型的音乐。假设我们想要拥有3种类型:Hip Hop, Rock和Country。为此,我们将使用类的名称来创建该类的新实例。

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)

现在我们已经创建了Genre类的实例,我们可以使用上面设置的“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 

我们可以通过编写以下代码将'g1'的名称设置为Hip Hop

g1.Name = "Hip Hop";

这里发生的事情有点复杂。如前所述,从私有字段获取和设置访问信息,否则您将无法访问这些字段。Get只能从私有字段读取信息并返回。Set只能写入该私有字段中的信息。但通过同时拥有get和set属性我们就能同时执行这两个函数。写出g1。名称= "嘻哈";我们特别使用了Name属性中的set函数

Set使用一个隐式变量value。基本上,这意味着任何时候你在set中看到value,它指的是一个变量;“value”变量。当我们写g1时。Name =我们使用=来传递值变量,在本例中为“Hip Hop”。所以你可以这样想:

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"'
    }
}

需要注意的是,上面的示例实际上并不是写在代码中。它更像是一个假想的代码,代表了后台正在发生的事情。

现在我们已经设置了类型的g1实例的名称,我相信我们可以通过写入来获得名称

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"

如果我们运行这个,我们会在控制台得到“Hip Hop”。

因此,为了便于解释,我将用输出完成示例

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

输出:

"Genres: Hip Hop, Rock, Country"

The get/set pattern provides a structure that allows logic to be added during the setting ('set') or retrieval ('get') of a property instance of an instantiated class, which can be useful when some instantiation logic is required for the property. A property can have a 'get' accessor only, which is done in order to make that property read-only When implementing a get/set pattern, an intermediate variable is used as a container into which a value can be placed and a value extracted. The intermediate variable is usually prefixed with an underscore. this intermediate variable is private in order to ensure that it can only be accessed via its get/set calls. See the answer from Brandon, as his answer demonstrates the most commonly used syntax conventions for implementing get/set.

在Visual Studio中,如果你在一个类中定义了一个属性X,并且你只想将这个类用作一个类型,在构建项目之后,你会得到一个警告,说“字段X从未被赋值,并且总是有它的默认值”。

通过添加{get;设置;}到X属性,则不会得到此警告。

此外,在Visual Studio 2013及更高版本中,通过添加{get;设置;}你就可以看到对该属性的所有引用。

这是做这件事的简短方式:

public class Genre
{
    private string _name;

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