将成员对象公开为 .NET 中的属性或方法
在 .NET 中,如果一个类包含一个类对象成员,那么该成员应该作为属性还是通过方法公开?
In .NET, if a class contains a member that is a class object, should that member be exposed as a property or with a method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您应该对概念上表示对象状态的任何内容使用属性,只要其检索不是一个昂贵的操作,您应该避免重复使用它。
来自 MSDN:
You should use properties for anything that conceptually represents the object's state, so long as its retrieval isn't an expensive enough operation that you should avoid using it repeatedly.
From MSDN:
财产。 属性基本上只是一种“廉价”方法。 获取或设置对象的引用非常便宜。
澄清一下,属性通常应该代表对象的内部状态。 然而,将成员实现为属性或方法会告诉用户调用的成本可能有多大。
Property. A Property is basically just a 'cheap' method. Getting or setting a reference to an object is pretty cheap.
Just to clarify, properties are usually supposed to represent the internal state of an object. However, the implementation of a member as a property or method tells the user how expensive the call is likely to be.
这与本案无关。
如果值是有关对象状态的一些详细信息,那么它应该是一个属性。
如果它对对象执行某些操作,则它应该是方法。
That is irrelevant to the matter.
It should be a Property if the value is some detail about the state of the object.
It should be Method if it performs some action on the object.
如果您所做的只是公开与当前对象状态相关的对象实例,则应该使用属性。
当您的逻辑不只是访问内存中的对象并返回该值,或者您正在执行对当前对象的状态有广泛影响的操作时,应该使用方法。
If all you are doing is exposing an object instance that is relevant to the state of the current object you should use a property.
A method should be used when you have some logic that is doing more than accessing an in memory object and returning that value or when you are performing an action that has a broad affect on the state of the current object.
概述
一般来说,属性存储对象的数据,例如名称,方法是可以要求对象执行的操作,例如移动或显示。 有时,哪些类成员应该是属性、哪些应该是方法并不明显 - 集合类 (VB) 的 Item 方法存储和检索数据,并且可以作为索引属性来实现。 另一方面,将 Item 实现为方法也是合理的。
语法
类成员的使用方式也可能是决定它应该表示为属性还是方法的决定因素。 从参数化属性检索信息的语法几乎与作为函数实现的方法所使用的语法相同。 但是,修改此类值的语法略有不同。
如果将类成员实现为属性,则必须按以下方式修改其值:
ThisObject.ThisProperty(Index) = NewValue
如果类成员实现为方法,则修改的值必须使用争论进行修改:
ThisObject.ThisProperty(Index, NewValue)
错误
尝试将值分配给只读属性将返回与类似的错误消息不同的错误消息调用一个方法。 正确实现的类成员会返回更容易解释的错误消息。
Overview
Generally, properties store data for an object, such as Name, and methods are actions an object can be asked to perform, such as Move or Show. Sometimes it is not obvious which class members should be properties and which should be methods - the Item method of a collection class (VB) stores and retrieves data and can be implemented as an indexed property. On the other hand, it would also be reasonable to implement Item as a method.
Syntax
How a class member is going to be used could also be a determining factor in whether it should be represented as a property or a method. The syntax for retrieving information from a parameterized property is almost identical to the syntax used for a method implemented as a function. However, the syntax for modifying such a value is slightly different.
If you implement the member of a class as a property, you must modify it's value this way:
ThisObject.ThisProperty(Index) = NewValue
if the class member is implemented as a method, the value being modified must be modified using an arguement:
ThisObject.ThisProperty(Index, NewValue)
Errors
An attempt to assign a value to a read-only property will return a different error message than a similar call to a method. Correctly implemented class members return error messages that are easier to interpret.
属性读取类中的实例并将值分配给该实例。
方法对分配给类的数据执行某些操作。
Properties read and assign values to instances within a class.
Methods do something with the data assigned to the class.
我之前对使用属性和方法感到困惑。 但现在我根据 使用此规则MSDN指南:
方法代表动作,属性代表数据。 属性应该像字段一样使用,这意味着属性不应在计算上复杂或产生副作用。 当它不违反以下准则时,请考虑使用属性而不是方法,因为经验不足的开发人员发现属性更容易使用。
I confused about the using property and method before. But now I am using this rule according to MSDN Guideline:
methods represent actions and properties represent data. Properties are meant to be used like fields, meaning that properties should not be computationally complex or produce side effects. When it does not violate the following guidelines, consider using a property, rather than a method, because less experienced developers find properties easier to use.