Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
304 views
in Technique[技术] by (71.8m points)

c# - 什么是{get; 组; } C#中的语法?(What is the { get; set; } syntax in C#?)

I am learning ASP.NET MVC and I can read English documents, but I don't really understand what is happening in this code:

(我正在学习ASP.NET MVC,可以阅读英文文档,但是我不太了解这段代码中发生了什么:)

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

What does this mean: { get; set; }

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

({ get; set; } ?)

  ask by kn3l translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It's a so-called auto property, and is essentially a shorthand for the following (similar code will be generated by the compiler):

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...