法VS物业在C#中 - 有什么区别 [英] Method vs Property in C# - what's the difference

查看:120
本文介绍了法VS物业在C#中 - 有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:

  属性VS方法

在方法,你可以输入一些code和性能了。比如我有一个属性名称。当类名称更改我想从数据库中获取一些数据,并改变我的对象的状态。我可以添加此code将我的财产的一部分。其他的解决办法是设置部分改为私有,并添加方法称为SETNAME并在此方法中添加我的code。

In method you can type some code and in properties too. For example I have a property Name. When class name changes I would like to get some data from database and change state of my object. I can add this code to set part of my property. Other solution is to change set part to private and add method called SetName and in this method add my code.

所以,有什么区别?当是时,它不是好放一些code到的getter / setter以及何时创建自己的方法,用来改变我的财产,我的课的其他部分呢?

So what is the difference? When is the point when it's not good to put some code to getter / setter and when to create own method that is used to change my property and other parts of my class?

推荐答案

下面是一套良好的指引何时使用性能VS从<一个方法href=\"http://dipankaronline.word$p$pss.com/2011/10/28/guidelines-for-when-to-use-properties-vs-methods-from-bill-wagner/\">Bill瓦格纳(固定链接)

Here is a good set of guidelines for when to use properties vs methods from Bill Wagner (fixed link)


  • 使用时,所有的这些都是真实的一个属性:
    该干将应该是简单的,因此不太可能抛出异常。注意,这意味着没有网络(或数据库)的访问。要么可能会失败,因此会抛出异常。

  • 他们应该不会对彼此依赖。请注意,这将包括设置一个属性,并让它影响到另一个。 (例如,设置了名字属性将影响由名字+姓性质隐含了这种相关性的只读FullName属性)

  • 他们应该以任意顺序可设置

  • getter方法​​不具有可观察到的副作用说明这一准则并不preclude在属性某些形式的懒惰的评价。

  • 的方法必须立即返回。 (请注意,此precludes,使一个数据库访问呼叫,web服务调用,或其他类似的操作特性)。

  • 如果该成员返回一个数组使用的方法。

  • 要吸气重复调用(中间没有code)项应返回相同的值。

  • 重复调用setter方法​​(使用相同的值)应该从单一的通话产量无明显差异。

  • Use a Property when all these are true: The getters should be simple and thus unlikely to throw exceptions. Note that this implies no network (or database) access. Either might fail, and therefore would throw an exception.
  • They should not have dependencies on each other. Note that this would include setting one property and having it affect another. (For example, setting the FirstName property would affect a read-only FullName property that composed the first name + last name properties implies such a dependency )
  • They should be settable in any order
  • The getter does not have an observable side effect Note this guideline doesn't preclude some forms of lazy evaluation in a property.
  • The method must always return immediately. (Note that this precludes a property that makes a database access call, web service call, or other similar operation).
  • Use a method if the member returns an array.
  • Repeated calls to the getter (without intervening code) should return the same value.
  • Repeated calls to the setter (with the same value) should yield no difference from a single call.

的get不应该返回到内部数据结构(见第23项)的引用。一种方法可以返回一个深层副本,并能避免此问题。

The get should not return a reference to internal data structures (See item 23). A method could return a deep copy, and could avoid this issue.

这篇关于法VS物业在C#中 - 有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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