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?
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.
Properties read and assign values to instances within a class.
Methods do something with the data assigned to the class.
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.
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:
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.
public string Name get { return name; } set { name = value; }
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.
Type type = // Get a type. for (int i = 0; i < type.Methods.Length; i++) { if (type.Methods[i].Name.Equals ("text")) { // Perform some operation. } }
The following example illustrates the correct use of properties and methods.
class Connection { // The following three members should be properties // because they can be set in any order. string DNSName {get{};set{};} string UserName {get{};set{};} string Password {get{};set{};} // The following member should be a method // because the order of execution is important. // This method cannot be executed until after the // properties have been set. bool Execute (); }
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.