我在某个地方看到了类似下面的内容,并想知道这是什么意思.我知道他们是getter和setter,但是想知道为什么字符串Type是这样定义的.谢谢你的帮助.
public string Type { get; set; }
Run Code Online (Sandbox Code Playgroud)
Jus*_*ner 158
这些是自动实现的属性(简称自动属性).
编译器将自动生成以下简单实现的等效项:
private string _type;
public string Type
{
get { return _type; }
set { _type = value; }
}
Run Code Online (Sandbox Code Playgroud)
Teo*_*gul 30
这是一个自动属性,它是这个的简写符号:
private string type;
public string Type
{
get { return this.type; }
set { this.type = value; }
}
Run Code Online (Sandbox Code Playgroud)
M.H*_*san 23
在C#6中:
现在可以将自动属性声明为字段:
public string FirstName { get; set; } = "Ropert";
Run Code Online (Sandbox Code Playgroud)
只读自动属性
public string FirstName { get;} = "Ropert";
Run Code Online (Sandbox Code Playgroud)
Sec*_*und 13
public string Type { get; set; }
Run Code Online (Sandbox Code Playgroud)
没有什么不同的
private string _Type;
public string Type
{
get { return _Type; }
set { _Type = value; }
}
Run Code Online (Sandbox Code Playgroud)
它是一个自动支持的属性,基本上相当于
private string type;
public string Type
{
get{ return type; }
set{ type = value; }
}
Run Code Online (Sandbox Code Playgroud)
您还可以使用 lambda 表达式
public string Type
{
get => _type;
set => _type = value;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
217487 次 |
最近记录: |