tags:

views:

1725

answers:

7

Hi,

I am working on creating an immutable class.
I have marked all the properties as read-only.

I have a list of items in the class.
Although if the property is read-only the list can be modified.

Exposing the IEnumerable of the list makes it immutable.
I wanted to know what is the basic rules one has to follow to make a class immutable ?

Thanks.

A: 

Try to use the System.Collections.ReadOnlyCollectionBase

JSC
+5  A: 

To be immutable, all your properties and fields should be readonly. And the items in any list should themselves be immutable.

You can make a readonly list property as follows:

public class MyClass
{
    public MyClass(..., IList<MyType> items)
    {
        ...
        _myReadOnlyList = new List<MyType>(items).AsReadOnly();
    }

    public IList<MyType> MyReadOnlyList
    {
        get { return _myReadOnlyList; }
    }
    private IList<MyType> _myReadOnlyList

}
Joe
+20  A: 

I think you're on the right track -

  • all information injected into the class should be supplied in the constructor
  • all properties should be getters only
  • if a collection (or Array) is passed into the constructor, it should be copied to keep the caller from modifying it later
  • if you're going to return your collection, either return a copy or a read-only version (for example, using ArrayList.ReadOnly or similar - you can combine this with the previous point and store a read-only copy to be returned when callers access it), return an enumerator, or use some other method/property that allows read-only access into the collection
  • keep in mind that you still may have the appearance of a mutable class if any of your members are mutable - if this is the case, you should copy away whatever state you will want to retain and avoid returning entire mutable objects, unless you copy them before giving them back to the caller - another option is to return only immutable "sections" of the mutable object - thanks to @Brian Rasmussen for encouraging me to expand this point
Blair Conrad
Should I write an wrapper look up property , which lets you only do the look up ? And thanks for the answer.
Biswanath
Any mutable reference type passed as an argument to the constructor should be copied. Otherwise the caller will still hold a reference to the state.
Brian Rasmussen
@Biswanath - I don't quite understand the question
Blair Conrad
@Brian Rasmussen, you're right, but even if it's copied, may be possible for any caller to access the mutable object, depending on the acces.sors provided. In the case of being passed a mutable object, the class is best off always returning a different copy, or immutable sections of the object
Blair Conrad
@Blair - If I have dictionary in the class which I just want to use it for the look up. Is a read-only property which does a look up should be fine ?
Biswanath
@Biswanath - yes, with the caveat that if the values in the dictionary are mutable, you will need to protect them before returning, if you don't want them mutated
Blair Conrad
+10  A: 

I strongly recommend that you read Eric Lippert's blog series on immutability, in particular the entry on "kinds of immutability".

Your comment that "Exposing the IEnumerable of the list makes it immutable" seems somewhat strange to me. What do you mean by it?

Jon Skeet
Thanks for pointing to a nice article.
Biswanath
It's strange to me too...
Daok
What I meant was, rather than allowing to access the list( If the object have a list of some other objects ), allowing the user to access the members with IEnumerable.Talking list here as in a specific example, but it can be any data structure.
Biswanath
+2  A: 

Also, keep in mind that:

public readonly object[] MyObjects;

is not immutable even if it's marked with readonly keyword. You can still change individual array references/values by index accessor.

lubos hasko
A: 

Another option would be to use a visitor pattern instead of exposing any internal collections at all.

Andrew
+2  A: 

Use the ReadOnlyCollection class. It's situated in the System.Collections.ObjectModel namespace.

On anything that returns your list (or in the constructor), set the list as a read-only collection.

using System.Collections.ObjectModel;

...

public MyClass(..., List<ListItemType> theList, ...)
{
    ...
    this.myListItemCollection= theList.AsReadOnly();
    ...
}

public ReadOnlyCollection<ListItemType> ListItems
{
     get { return this.myListItemCollection; }
}
GoodEnough