开发者

"Tell, don't ask": what is the difference between a non-void method and a getter?

How might I justify using a non-void method while not using any property getters? What is the distinction between these two concepts such that getters are evil but non-void methods are acceptable?

EDIT:

int CalculateSomething();

int Calculation { get; }

The fact that I can change the开发者_运维技巧 signature of CalculateSomething and pass values into it if I wanted to completely slipped my mind. So my question is changed to: Is the fundamental distinction between getters and non-void methods that arguments can be passed into non-void methods?


Microsoft has guidance for choosing between properties and methods.

  • Consider using a property if the member represents a logical attribute of the type.
  • Do use a property, rather than a method, if the value of the property is stored in the process memory and the property would just provide access to the value.
  • Do use a method, rather than a property, in the following situations.
    • The operation is orders of magnitude slower than a field set would be. If you are even considering providing an asynchronous version of an operation to avoid blocking the thread, it is very likely that the operation is too expensive to be a property. In particular, operations that access the network or the file system (other than once for initialization) should most likely be methods, not properties.
    • The operation is a conversion, such as the Object.ToString method.
    • The operation returns a different result each time it is called, even if the parameters do not change. For example, the NewGuid method returns a different value each time it is called.
    • The operation has a significant and observable side effect. Note that populating an internal cache is not generally considered an observable side effect.
    • The operation returns a copy of an internal state (this does not include copies of value type objects returned on the stack).
    • The operation returns an array.


Isn't the big difference, that you can't pass a variable to a getter?

a function:

private int Getyear(datetime date) { }

a property:

private DateTime GetDate {
    get {return _date};
}

you can't do that with a getter property can you?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜