首页 > C# > 在c#中,这是什么意思: ?{ get; set; }

在c#中,这是什么意思: ?{ get; set; }

上一篇 下一篇

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

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

这是什么意思: ?{ get; set; }

分割线

网友回答:

这是一个所谓的 auto 属性,本质上是以下内容的简写(编译器将生成类似的代码):

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

分割线

网友回答:

因此,据我了解,它是一个“自动属性”,就像@Klaus和@Brandon所说的那样,它是编写带有“支持字段”的属性的简写。所以在这种情况下:{ get; set; }

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

不可否认,上述陈述非常令人困惑,所以让我们来看一些例子。假设此代码指的是音乐类型。因此,在类流派中,我们将想要不同类型的音乐。假设我们想要 3 种类型:嘻哈、摇滚和乡村。为此,我们将使用 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)

现在我们已经创建了 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";

这里发生的事情有点复杂。就像我之前说的,从私人字段中访问您无法访问的信息。 只能从该私有字段中读取信息并将其返回。 只能在该私有字段中写入信息。但是,通过同时拥有两者的属性,我们能够同时执行这两个功能。通过编写,我们专门使用来自 Name 属性的函数getsetgetsetgetsetg1.Name = "Hip Hop";set

set使用名为 的隐式变量。基本上这意味着任何时候你在 中看到“值”,它指的是一个变量;“值”变量。当我们编写时,我们使用 to 传入变量,在本例中为 .所以你基本上可以这样想:valuesetg1.Name ==value"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"'
    }
}

请务必注意,上面的示例实际上并未编写在代码中。它更像是一个假设的代码,代表后台发生的事情。

所以现在我们已经设置Genre 的 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"

分割线

网友回答:

这些是自动属性

基本上是编写带有支持字段的属性的另一种方式。

public class Genre
{
    private string _name;

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

模板简介:该模板名称为【在c#中,这是什么意思: ?{ get; set; }】,大小是暂无信息,文档格式为.编程语言,推荐使用Sublime/Dreamweaver/HBuilder打开,作品中的图片,文字等数据均可修改,图片请在作品中选中图片替换即可,文字修改直接点击文字修改即可,您也可以新增或修改作品中的内容,该模板来自用户分享,如有侵权行为请联系网站客服处理。欢迎来懒人模板【C#】栏目查找您需要的精美模板。

相关搜索
  • 下载密码 lanrenmb
  • 下载次数 175次
  • 使用软件 Sublime/Dreamweaver/HBuilder
  • 文件格式 编程语言
  • 文件大小 暂无信息
  • 上传时间 04-09
  • 作者 网友投稿
  • 肖像权 人物画像及字体仅供参考
栏目分类 更多 >
热门推荐 更多 >
微信图片 响应式 企业网站 微信文章 微信公众平台 微信模板 自适应 单页式简历模板 微信素材 html5
您可能会喜欢的其他模板