什么准则适合于确定何时实现类成员作为一个属性与方法? [英] What guidelines are appropriate for determining when to implement a class member as a property versus a method?

查看:164
本文介绍了什么准则适合于确定何时实现类成员作为一个属性与方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的<一个href="http://submain.com/landing/$c$cit.right/?utm_campaign=$c$cit.right&utm_medium=textad&utm_source=stackoverflow"相对=nofollow> .NET的编码标准从已经开始出现在该文件的赞助商通过区域似乎表明属性仅适宜用于逻辑数据成员(见34-35页上SubMain 的PDF )。方法在以下情况下被认为适当的:

The .NET coding standards PDF from SubMain that have started showing up in the "Sponsored By" area seems to indicate that properties are only appropriate for logical data members (see pages 34-35 of the document). Methods are deemed appropriate in the following cases:

  • 的操作的转换,如Object.ToString()。
  • 的操作是要传达给他们应考虑缓存结果的用户够贵。
  • 获取使用get访问将有一个观察到的副作用的属性值。
  • 在连续调用两次成员会产生不同的结果。
  • 执行的顺序是非常重要的。
  • 的成员是静态的,但是返回可以更改的值。
  • 在该成员返回一个数组。
  • The operation is a conversion, such as Object.ToString().
  • The operation is expensive enough that you want to communicate to the user that they should consider caching the result.
  • Obtaining a property value using the get accessor would have an observable side effect.
  • Calling the member twice in succession produces different results.
  • The order of execution is important.
  • The member is static but returns a value that can be changed.
  • The member returns an array.

难道大多数开发商同意上面​​的属性与方法的参数?如果是这样,为什么呢?如果不是,为什么不呢?

Do most developers agree on the properties vs. methods argument above? If so, why? If not, why not?

推荐答案

他们好像声音,并基本符合MSDN成员设计的指导方针:

They seem sound, and basically in line with MSDN member design guidelines:

http://msdn.microsoft.com/en-us/library/ ms229059.aspx

有一点,人们似乎有时会忘记(*)的是,来电者应当能够按照任何顺序设置属性。为支持设计师,因为你不能确定的产生顺序code将设置属性的类尤其重要。

One point that people sometimes seem to forget (*) is that callers should be able to set properties in any order. Particularly important for classes that support designers, as you can't be sure of the order generated code will set properties.

(*)我记得Ajax控件工具包在codePLEX早期版本已经由于开发商忘记了这个许多错误。

(*) I remember early versions of the Ajax Control Toolkit on Codeplex had numerous bugs due to developers forgetting this one.

至于连续调用两次成员会产生不同的结果,每一个规则都有例外,因为酒店DateTime.Now说明。

As for "Calling the member twice in succession produces different results", every rule has an exception, as the property DateTime.Now illustrates.

这篇关于什么准则适合于确定何时实现类成员作为一个属性与方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆