I was just thinking about it and since .Net has introduced properties is there ever a situation where you would want to leave your code as a method that returns a value as opposed to a readonly property.
If it's a parameterless method, then it's worth considering migrating to a property, yes. If it's just returning a private member variable, then it should definitely be a property.
But in general, no, not every method that returns a value should be a property. Some methods instantiate objects and return them, and those should never be properties. Some methods return this allowing for a "Fluent" API. None of these should be properties.
No. Methods that return something, and perform expensive operations should not be modified into a property for instance.
Methods that take parameters cannot be turned into a property.
Methods that return arrays should not be transformed into a property for instance. That is because in such cases , you should return a copy of the array in order to prohibit that someone modifies the array outside the class. When you use a property that returns a copy of an array, then programmers are more likely to write something like this:
foreach( var x in myClass.PropertyThatReturnsArray )
{
}
For every iteration, an new copy of the array will be created. Instead, if you have written this as a method, programmers will more likely write this:
var foo = myClass.GetTheArray();
foreach( var x in foo )
{
}
(This is a rule that has been enforced by fxcop : http://msdn.microsoft.com/en-us/library/0fss9skc(VS.80).aspx ).
It's a psychological thing. :)
No. I'd recommend taking a look at Microsoft's Property Usage Guidlines:
Class library designers often must decide between implementing a class member as a property or a method. In general, methods represent actions and properties represent data. Use the following guidelines to help you choose between these options.
- Use a property when the member is a logical data member. In the following member declarations, Name is a property because it is a logical member of the class.
- Use a method when:
- 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. Note that a type's properties should be able to be set and retrieved in any order.
- The member is static but returns a value that can be changed.
- The member returns an array. Properties that return arrays can be very misleading. Usually it is necessary to return a copy of the internal array so that the user cannot change internal state. This, coupled with the fact that a user can easily assume it is an indexed property, leads to inefficient code. In the following code example, each call to the Methods property creates a copy of the array. As a result, 2n+1 copies of the array will be created in the following loop.