http://msdn2.microsoft.com/en-US/lib...57(VS.80).aspx
* Do not assign instances of mutable types to read-only fields.
I would have to disagree with this "Field Design" guidelines...to an extent.
Example:
public class SomeClass {
private NameValueCollection mCollection;
public SomeClass() { mCollection = new NameValueCollection(); }
public NameValueCollection Collection {
get { return mCollection; }
}
public int CollectionCount()
{
return mCollection.Count;
}
}
For the above (granted, there may be some errors as this is just off top of
my head), the CollectionCount method requires that the mCollection field be
set. I expose the collection to the callee using a read-only property. The
property returns the mutable type just as I want. In a scenerio for my
example, the callee is allowed to change the internal collection values
(add/remove items to a list). But, if the user was allowed to modify the
actual reference, they could set it to null which would cause other areas of
the class (the mCollection.Count) to throw NullReferenceException's.
Therefore, am I not correct that in this instance, the mutable type being
returned by a read-only field is an exception to the rule?
Thanks,
Mythran