2023-06-02 09:00:01

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

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

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

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


当前回答

这是一种将数据成员公开为公共的快捷方式,这样您就不需要显式地创建私有数据成员。c#将为您创建一个私有数据成员。

您可以不使用此快捷方式将数据成员设为公共,但如果您决定更改数据成员的实现以具有某些逻辑,则需要破坏接口。因此,简而言之,它是创建更灵活代码的捷径。

其他回答

属性是用于封装数据的函数,并允许在每次检索或修改值时执行额外的代码。

c#不同于c++, VB。Net或Objective-C没有一个单独的关键字来声明属性,而是使用两个关键字(get/set)来给出一个简短的语法来声明函数。

But it is quite common to have properties, not because you want to run additional code when data is retrieved or modified, but because either you MIGHT want to do so in the future or there is a contract saying this value has to be a exposed as a property (C# does not allow exposing data as fields via interfaces). Which means that even the abbreviated syntax for the functions is more verbose than needed. Realizing this, the language designers decided to shorten the syntax even further for this typical use case, and added “auto” properties that don’t require anything more than the bare minimum, to wit, the enclosing braces, and either of the two keywords (separated by a semicolon when using both).

在VB。Net中,这些“auto”属性的语法与c#中的长度相同——属性X为字符串vs字符串X {get;Set;},两种情况下都是20个字符。它实现了这样的简洁,因为在正常情况下,它实际上需要3个关键字,而在auto属性的情况下,可以不需要其中的2个。

如果从这两者中删除更多,要么就必须添加一个新的关键字,要么就必须赋予符号或空白以意义。

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

public class Genre
{
    private string _name;

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

属性就像一个层,它将私有变量与类的其他成员分开。从外界看来,属性只是一个字段,可以使用.Property访问属性

public class Person
{
    public string FirstName { get; set; }

    public string LastName { get; set; }

    public string FullName => $"{FirstName} {LastName}";
}

public class Person
{
    public string FirstName { get; set; }

    public string LastName { get; set; }

    public string FullName { get { return $"{FirstName} {LastName}"; } }
}

FullName是一个属性。有箭头的那个是捷径。从外部世界,我们可以像这样访问FullName:

var person = new Person();
Console.WriteLine(person.FullName);

调用者并不关心您如何实现FullName。但在类内部,你可以随心所欲地更改FullName。

查看微软文档以获得更详细的解释:

https://learn.microsoft.com/en-us/dotnet/csharp/properties

它基本上是一种速记。你可以写公共字符串Name {get;设置;}就像在许多例子中一样,但你也可以这样写:

private string _name;

public string Name
{
    get { return _name; }
    set { _name = value ; } // value is a special keyword here
}

为什么使用它?它可以用来过滤对属性的访问,例如您不希望名称中包含数字。

让我给你们举个例子:

private class Person {
    private int _age;  // Person._age = 25; will throw an error
    public int Age{
        get { return _age; }  // example: Console.WriteLine(Person.Age);
        set { 
            if ( value >= 0) {
                _age = value; }  // valid example: Person.Age = 25;
        }
    }
}

它的官方名称是Auto-Implemented Properties,阅读(编程指南)是一个好习惯。 我还推荐教程视频c#属性:为什么使用“get”和“set”。

所以在我看来{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"