C# 自动实现的属性帮助
C# Auto-Implemented Properties Assistance
在 C# 中,我对属性感到困惑。特别是当您将 get 和 set 设置为 { get; set;}
时。你这样做是什么意思?例如我有 class 属性:
public Dictionary<string, string> clientDict { get; set; }
我见过它们实际定义 get 和 set 的属性。我也明白删除 set 会使它只读。但是在这种情况下会发生什么?它只使用普通字典的默认访问器吗?
简化它。如果它是 int
而不是 Dictionary
是什么意思?
public int ClientInt {get;set;}
int
没有访问器,所以您不会问这个问题。
public Dictionary<string, string> clientDict { get; set; }
相当于手动定义get
和set
。编译器在幕后为您处理所有这些。所以你的上面会变成:
private Dictionary<string, string> _clientDict;
public Dictionary<string, string> clientDict;
{
get { return _clientDict; }
set { _clientDict = value; }
}
您可能会将 class 成员 的访问器与 集合 的访问器混淆了。正如评论者指出的那样,Auto-Implemented Properties 上的 MSDN 页面解释说:
public Dictionary<string, string> clientDict { get; set; }
相当于:
private Dictionary<string, string> _clientDict;
public Dictionary<string, string> clientDict
{
get { return _clientDict; }
set(Dictionary<string, string> value)
{
_clientDict = value;
}
}
那些访问器只是 return 对集合的引用。它们没有传递任何东西(正如您的问题 "Does it just use the default accessors of a normal dict?" 所暗示的),并且它们与 Dictionary<T>
class 的 []
和 .Add()
方法无关.
当您通过 属性 访问词典时:
var foo = clientDict["SomeKey"];
那将首先returnclientDict
*属性的访问结果,即对_clientDict
的引用,然后索引到那个字典, returning 结果值(假设键存在)并将其分配给 foo
.
如果还有什么让您对自动属性感到困惑,请评论或编辑问题。
* 顺便说一句,我竭尽全力不把 ClientDict
写成 属性 的名称,因为 C# 约定是大写 属性 名称就像方法名称一样:)
- 这叫做"Automatic Properties"。 (当你只写 set 和 get 方法,里面没有任何代码时)
- 自动属性的目标是简化编码过程,并使其更快。
当你像上一个一样写属性时。
public 字典 clientDict { get;放; }
编译器将其翻译成以下内容
private Dictionary<string, string> _clientDic;
public Dictionary<string, string> clientDic
{
get { return _clientDic; }
set { _clientDic = value; }
}
当你像下面这样写 属性 时
public int X {get;}
编译器将其翻译成以下内容
private int _x;
public int X{
get { return _x; }
}
In C# I'm confused about properties. Specifically when you set get and
set as just { get; set;}. What does it mean when you do that?
这意味着您正在定义一个 Automatic Property
,在幕后,编译器会创建一个私有的、匿名的 backing field
,它只能通过 属性 的 get 访问并设置访问器。
所以,你的下一个问题可能是...
What's a backing field
?
A backing field
是 field
,property
引用存储和检索值。
您已经看到了 automatic property
的样子,这是 property
和 backing field
的样子...
private string name; // Backing field
public string Name // Property
{
get { return this.name; }
set { this.name = value }
}
I've seen properties where they actually define get and set. I also
understand that removing set makes it read only.
是的,它使 Property
成为只读的,但要小心,因为 C#
还包含一个名为 readonly
的 keyword
,它设置在 Fields
.例如。 private readonly string name;
..
正如属性可以是read only
一样,它们也可以是write only
,如果你去掉Get
访问器,只留下Set
访问器,客户将只能写入它。
But what happens in this case? Does it just use the default accessors of a normal dict?
您在上面给出的字典示例将有一个支持字段...客户端将能够写入和读取它。
不过,如果我们愿意,我们可以进行更改...
想象一下,我们正在提供一场现场足球比赛的比分,让 write
访问客户端将是一场噩梦......所以我们可以限制它并只公开一个 get
访问器,同时保持 set
访问器私有。
public int TeamFoo { get; private set; }
public int TeamBar { get; private set; }
或者,我们可以使用明确定义的支持字段来实现...
private int teamFoo;
public int TeamFoo { get { return teamFoo; } }
在 C# 中,我对属性感到困惑。特别是当您将 get 和 set 设置为 { get; set;}
时。你这样做是什么意思?例如我有 class 属性:
public Dictionary<string, string> clientDict { get; set; }
我见过它们实际定义 get 和 set 的属性。我也明白删除 set 会使它只读。但是在这种情况下会发生什么?它只使用普通字典的默认访问器吗?
简化它。如果它是 int
而不是 Dictionary
是什么意思?
public int ClientInt {get;set;}
int
没有访问器,所以您不会问这个问题。
public Dictionary<string, string> clientDict { get; set; }
相当于手动定义get
和set
。编译器在幕后为您处理所有这些。所以你的上面会变成:
private Dictionary<string, string> _clientDict;
public Dictionary<string, string> clientDict;
{
get { return _clientDict; }
set { _clientDict = value; }
}
您可能会将 class 成员 的访问器与 集合 的访问器混淆了。正如评论者指出的那样,Auto-Implemented Properties 上的 MSDN 页面解释说:
public Dictionary<string, string> clientDict { get; set; }
相当于:
private Dictionary<string, string> _clientDict;
public Dictionary<string, string> clientDict
{
get { return _clientDict; }
set(Dictionary<string, string> value)
{
_clientDict = value;
}
}
那些访问器只是 return 对集合的引用。它们没有传递任何东西(正如您的问题 "Does it just use the default accessors of a normal dict?" 所暗示的),并且它们与 Dictionary<T>
class 的 []
和 .Add()
方法无关.
当您通过 属性 访问词典时:
var foo = clientDict["SomeKey"];
那将首先returnclientDict
*属性的访问结果,即对_clientDict
的引用,然后索引到那个字典, returning 结果值(假设键存在)并将其分配给 foo
.
如果还有什么让您对自动属性感到困惑,请评论或编辑问题。
* 顺便说一句,我竭尽全力不把 ClientDict
写成 属性 的名称,因为 C# 约定是大写 属性 名称就像方法名称一样:)
- 这叫做"Automatic Properties"。 (当你只写 set 和 get 方法,里面没有任何代码时)
- 自动属性的目标是简化编码过程,并使其更快。
当你像上一个一样写属性时。
public 字典 clientDict { get;放; }
编译器将其翻译成以下内容
private Dictionary<string, string> _clientDic;
public Dictionary<string, string> clientDic
{
get { return _clientDic; }
set { _clientDic = value; }
}
当你像下面这样写 属性 时
public int X {get;}
编译器将其翻译成以下内容
private int _x;
public int X{
get { return _x; }
}
In C# I'm confused about properties. Specifically when you set get and set as just { get; set;}. What does it mean when you do that?
这意味着您正在定义一个 Automatic Property
,在幕后,编译器会创建一个私有的、匿名的 backing field
,它只能通过 属性 的 get 访问并设置访问器。
所以,你的下一个问题可能是...
What's a
backing field
?
A backing field
是 field
,property
引用存储和检索值。
您已经看到了 automatic property
的样子,这是 property
和 backing field
的样子...
private string name; // Backing field
public string Name // Property
{
get { return this.name; }
set { this.name = value }
}
I've seen properties where they actually define get and set. I also understand that removing set makes it read only.
是的,它使 Property
成为只读的,但要小心,因为 C#
还包含一个名为 readonly
的 keyword
,它设置在 Fields
.例如。 private readonly string name;
..
正如属性可以是read only
一样,它们也可以是write only
,如果你去掉Get
访问器,只留下Set
访问器,客户将只能写入它。
But what happens in this case? Does it just use the default accessors of a normal dict?
您在上面给出的字典示例将有一个支持字段...客户端将能够写入和读取它。
不过,如果我们愿意,我们可以进行更改...
想象一下,我们正在提供一场现场足球比赛的比分,让 write
访问客户端将是一场噩梦......所以我们可以限制它并只公开一个 get
访问器,同时保持 set
访问器私有。
public int TeamFoo { get; private set; }
public int TeamBar { get; private set; }
或者,我们可以使用明确定义的支持字段来实现...
private int teamFoo;
public int TeamFoo { get { return teamFoo; } }