为什么应该在属性访问器中使用私有变量?
抱歉,如果我是菜鸟,我有这个疑问,为什么我们使用私有变量并使用属性设置它们?
为什么我们不能单独使用属性?
我正在谈论这样的情况,
private string _testVariable;
public string MyProperty
{
get { return _testVariable;}
set {_testVariable = value;}
}
我正在考虑简单地使用
public string MyProperty { get; set; }
为什么冗余私有变量?这两种策略有什么不同吗?谁能解释一下这一点。
谢谢
Sorry If I am being noob, I have this doubt, why do we use private variables and set them using properties ?
Why can't we just use properites alone ?
I am talking about situations like this
private string _testVariable;
public string MyProperty
{
get { return _testVariable;}
set {_testVariable = value;}
}
I am thinking of simply using
public string MyProperty { get; set; }
Why redundant private variable? are these two strategies different ? can anyone please throw some light on this.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您的示例在语义上是相同的。简洁的属性声明语法(只有
{ get; set; }
)是 C# 3.0 中的快捷方式。编译器实际上创建了一个私有支持变量和一个简单的 getter 和 setter,如第一个示例中所示。如果您所做的只是创建一个 getter 和 setter(并且当其中任何一个发生时实际上什么也没有发生),那么简洁的语法是一个不错的选择。如果在设置值时必须执行任何其他操作(例如重绘控件),则需要完整的语法。
Your examples are semantically the same. The concise property declaration syntax (just having
{ get; set; }
) is a shortcut available in C# 3.0. The compiler actually creates a private backing variable and a simple getter and setter as in your first example.If all you're doing is creating a getter and setter (and nothing actually happens when either occurs), then the concise syntax is a good option. If you have to perform any other actions (redraw a control, for example) when you set the value, then the full syntax is required.
如果您所做的只是读/写变量,那么不行。否则,您需要私有变量有两个原因:
数据验证
Getter/setter 包装底层数据存储
If all your doing is reading/writing a variable, then no. Otherwise, there's two reasons why you'd want a private variable:
Data validation
Getter/setter wraps up an underlying data store
您给出的第二个示例:
仅在 .Net 框架的更高版本中可用(我相信是 v3.0 以后)
第一个示例允许您在
return
和赋值语句上设置断点,从而导致您的调试器在分配/读取属性时中断。The second example that you give:
Is only available in later versions of the .Net framework (v3.0 onwards I believe)
The first example allows you to set breakpoints on the
return
and assignment statements, causing your debugger to break when the property is assigned / read.第一个代码片段允许您修改一些私有类状态。将私有状态包装在属性中很好,因为它隐藏了实现。稍后您可以更改实现,并且属性(外部接口)可能保持不变。
例如,假设您不是在 setter 中设置单个字符串,而是在某种私有存储中设置该字符串。您将其写入文件,或将其写入共享内存。或者,您可能只计算字符串的哈希值,根本不存储它,就像使用密码一样。
第二个代码片段中的自动属性与私有变量根本不相关。自动属性设计与第一个片段中使用的显式属性设计一样,允许将来进行修改。例如,作为修改的一部分,您可以从自动属性转换为显式实现的属性。
The 1st code snip allws you to modify some private class state. Wrapping private state in a property is nice because it hides the implementation. Later you can change the implementation and the property (external interface) may remain unchanged.
For example, suppose instead of setting a single string within the setter, you set the string in a private store of some sort. You write it to a file, or write it to shared memory. Or maybe you compute the hash of the string only, and don't store it at all, as you might do with a password.
The auto properties in your 2nd code snip are not related to the private variable at all. The auto-property design, like the explicit property design used in the first snip, allows future modification. As part of that modification, for example, you could convert from auto properties to explicitly implemented properties.
马什什,
我们都必须从某个地方开始!您通过以下方式询问了私有变量与属性:
您是否考虑过:
您可以将范围应用于属性 getters/setters 。 。 。 。很酷的东西。哦,是的。 。 。在定义类中(例如在构造函数中)使用此类属性时,请在其前面添加“this”。 - 所以分配看起来像'this.MyProperty = "An Assigned String";'。这会让您的意图更加更加明确。 。 。
Mashesh,
We all had to start somewhere! You asked about private vars vs properties with this ex:
Did you consider:
You can apply scope to property getters/setters . . . . cool stuff. Oh yeah . . . when using this type of property within the defining class (like in a constructor) prepend it with a 'this.' - so an assignment would look like 'this.MyProperty = "An Assigned String";'. This makes your intentions much more clear . . .
属性基本上是字段的包装。该包装器允许使用来自外部世界的变量。在 C#3.0 中,您可以简单地声明一个属性,如
public string MyProperty { get;放;编译器会自动声明一个私有变量并为其获取设置方法。如果您需要在声明属性的类内执行任何计算,那么您应该使用私有字段。
Property is basically the wrapper around a field. This wrapper enables to use the variable from outside world. In C#3.0 you can simply declare a property like
public string MyProperty { get; set; }
The compiler declares a private variable automatically and get set methods for that also. If you need to perform any calculation inside the class that is declaring the property, then you should use the private field for that.有时,您不知道第一次编写代码时是否会在以后添加更多需要使用私有变量的代码。当然,如果需要,您可以稍后添加。我只是自动创建私有变量,假设稍后会使用它。
这在大型企业应用程序或快速发展的应用程序(敏捷)中可能更相关,在这些应用程序中,在初始编码期间可能不知道完整的实现。
Sometimes you don't know when you first write the code whether you may be adding some more code later that needs to use the private variable. Sure, you can add it later if needed. I just automatically create the private variable, assuming that it will be used later.
This may be more relevant in large enterprise apps or rapidly evolving apps (agile) where the full implementation may not be known during the initial coding.
我讨厌在不需要时支持变量,这会导致比必要的更加复杂。
显然,如果您需要在 getter 或 setter 中做一些特殊的事情,那么应该使用完整的语义形式而不是糖。
另外,我喜欢使用属性作为调试属性如何设置或使用的方法,有时由于反射,这并不那么明显,这就是我喜欢使用它们的原因之一。
当支持变量可能在类中通过其自身的属性或支持变量在类内部访问并且没有任何东西告诉编码器正确的访问方式时,我发现尝试调试代码令人沮丧。
您可以在内部访问支持变量以及属性,那么哪种方法是正确的?这并不明显...
I HATE backing variables when they are not needed it is causes more complexity then necessary.
Obviously if you have the need to do something special in the getter or setter then the full semantic form should be used and not the sugar.
Also I like to use properties as a method of debugging how the property gets set or gets used sometimes this isn't as obvious because of reflection and that is one reason I like to use them.
I find it frustrating trying to debug code when there is a possibility that the backing variable maybe accessed either internally in the class by the property it self or the backing variable and nothing tells the coder the right way to access.
You can access the backing variable internally as well as the property so which is the right way? It is not obvious...
这与 C# 语言无关,而更多的是应用程序。
使用属性的原因之一是它在许多框架中被视为“特殊”。
例如,Silverlight 和 WPF 将绑定到属性而不是字段
This isnt related to C# the langugage, but more the application.
One reason to use properties, is that its treated as "Special" in many frameworks.
For example, Silverlight and WPF will bind to properties and not to fields