I have methods returning private collections to the caller and I want to prevent the caller from modifying the returned collections.
private readonly Foo[] foos;
public IEnumerable<Foo> GetFoos()
{
return this.foos;
}
At the moment the private collection is a fixed array, but in the future the collection might become a list if the need for adding new items at run time arises.
There are several solutions to prevent the caller from modifying the collection. Returning IEnumerable<T>
is the simplest solution, but the caller can still up-cast the return value to IList<T>
and modify the collection.
((IList<Foo>)GetFoos())[0] = otherFoo;
Cloning the collections has the obvious disadvantage that there are two collections that can evolve independently. So far I have considered the following options.
- Wrapping the collection in
ReadOnlyCollection<T>
. - Returning one of the LINQ iterators defined by the
Enumerable
class by performing a dummy projection likelist.Select(item => item)
. Actually I consider usingWhere(item => true)
because the returned iterator seems more lightweight. - Writing a custom wrapper.
What I don't like about using ReadOnlyCollection<T>
is that it implements IList<T>
and calling Add()
or accessing the indexer will cause exceptions. While this is absolutly correct in theory, almost no real code checks IList<T>.IsReadOnly
or IList<T>.IsFixedSize
.
Using the LINQ iterators - I wrapped the code in an extension method MakeReadOnly()
- prevents this scenario, but it has the taste of a hack.
Writing a custom wrapper? Reinventing the wheel?
Any thoughts, considerations, or other solutions?
While tagging this question, I discovered this Stack Overflow question I didn't notice before. Jon Skeet suggest to use the "LINQ hack", too, but even more efficient using Skip(0)
.