属性和自动实现

Properties and auto-implementations

我在 Outlook 2013 的 VSTO 插件中使用 .NET 4.5。我在完全掌握属性和访问器方面遇到了一些麻烦。自动实现的访问器,我假设是在你只写 get 的时候;放;而不是 get { //code } 等也给我带来了麻烦。我在 class 中有一本内部使用的字典。这是我的代码:

private Dictionary<string, string> clientDict { get; set; }
private Dictionary<string, string> clientHistoryDict { get; set; }

然后稍后:

clientDict = new Dictionary<string, string>();
clientHistoryDict = new Dictionary<string, string>();

我在稍后的代码中使用与属性相同的名称,在相同的 class.

我从来没有写过:

private Dictionary<string, string> _clientDict; // etc.

创建变量我只是直接使用 属性。

我尝试更改我的代码来执行此操作,但我遇到了一些问题并意识到我对属性的理解有点混乱。

这里有几个我需要澄清的问题,我似乎找不到正确的答案。

首先,有没有理由使用私有属性?我的词典永远不会在 class 之外或任何派生的 classes 中访问,所以有理由使用属性吗?我不使用任何特殊验证或 setter 中的任何东西或类似的东西。

其次,当我尝试更改我的代码以使用变量,然后像典型的 属性 示例那样通过属性访问它们时,我 运行 遇到了问题。我找到了一个示例,其中 getter 设置为 return _clientDict,但 setter 只是 set; 它给了我一个错误:我必须给 set 一个主体,因为它不是抽象的或局部的。在这种情况下,为什么它不为我自动实现 setter?

最后,当我在声明它的 class 中对属性调用 new 时,使用 属性 和相同类型的普通变量有什么区别?在那种情况下,属性与变量有什么不同吗?当应该使用私有变量完成时,以这种方式使用属性是否是一种不好的做法?

这些可能是一些被误导的问题,但我找不到任何其他地方有信息来帮助我理解这些区别。我一直在研究属性,试图弄清楚所有这些,但我可以使用我的帮助。

First, is there any reason to use a private property?

通常不会。属性非常适合 封装 。使用 属性 的一个优点(有 many more)是它可以在赋值之前进行验证。当您拥有某些东西 private 时,您通常 不需要 保护它免受 您自己 的伤害。此外,属性具有设置不同访问器(privateprotected 等)的优势,而字段则没有。

Why would it not auto-implement the setter for me in this instance?

我们必须明白,自动实现的属性并不是黑魔法。编译器将为我们生成一个私有的支持字段,而不是我们自己提供。从他的角度来看,他看到您有一个 getter,return 是一个私有字段,但是 setter 是自动的,通常会指示某种 逻辑错误 在你的代码中。为什么你 return 一个值却设置一个完全不同的值?当您创建带有支持字段的 属性 时,您必须同时提供 getter 和 setter,those are the rules.

when I call new on the properties in the same class that it is declared in, what is the difference between doing that with a property and a normal variable of the same type?

语义上,没有new 属于正在构造的类型,将发出构造函数调用。不同之处在于一旦新创建的对象被分配。字段将导致编译器发出 stfld 操作码。对于 属性,它将发出 call 来调用 属性 setter。当你 access a 属性 时,编译器最终会在字段上调用 ​​get_YourPropertyName vs ldfld

Is it bad practice to use properties this way when it should be accomplished with private variables?

我不会称之为不好的做法,但我会觉得拥有一个私人 属性.

有点奇怪

有关字段和属性的更多见解,请参阅 What is the difference between a Field and a Property in C#?

Is there any reason to use a private property?

不 - 这就是自动实施的全部意义所在。当您只想获取或设置私有成员变量中的内容时,它使您不必编写所有额外代码。 .Net 在幕后处理阴影私有成员变量的创建。

When I tried to change my code to use variables and then access them via the properties like your typical property example would, I ran into problems. I found an example where the getter was set to return _clientDict, but the setter was just set; It gave me the error: that I must give set a body because it's not abstract or partial. Why would it not auto-implement the setter for me in this instance?

我的理解是,自动实施要么全有,要么全无。 (尽管在那里可以更正)。也就是说,我已经看到使用简单定义为 set { } 的 set 块编译代码。 编辑:只是为了澄清 set { } 块实际上不会设置值,它实际上吞没了调用并且什么都不做 - 尽管它会编译。

When I call new on the properties in the same class that it is declared in, what is the difference between doing that with a property and a normal variable of the same type? Do properties differ at all from variables in that case? Is it bad practice to use properties this way when it should be accomplished with private variables?

据我所知,没有真正的区别。完全相同的事情正在发生,只是 .Net 正在为您处理管道。