views:

1812

answers:

5

Is there any reason to expose an internal collection as a ReadOnlyCollection rather than an IEnumerable if the calling code only iterates over the collection?

class Bar
{
    private ICollection<Foo> foos;

    // Which one is to be preferred?
    public IEnumerable<Foo> Foos { ... }
    public ReadOnlyCollection<Foo> Foos { ... }
}


// Calling code:

foreach (var f in bar.Foos)
    DoSomething(f);

As I see it IEnumerable is a subset of the interface of ReadOnlyCollection and it does not allow the user to modify the collection. So if the IEnumberable interface is enough then that is the one to use. Is that a proper way of reasoning about it or am I missing something?

Thanks /Erik

+7  A: 

If you do this then there's nothing stopping your callers casting the IEnumerable back to ICollection and then modifying it. ReadOnlyCollection removes this possibility, although it's still possible to access the underlying writable collection via reflection. If the collection is small then a safe and easy way to get around this problem is to return a copy instead.

Stu Mackellar
That's the kind of "paranoid design" I heartily disagree with. I think it's bad practice to choose inadequate semantics in order to enforce a policy.
Vojislav Stojkovic
You disagreeing doesn't make it wrong. If I'm designing a library for external distribution I'd much rather have a paranoid interface than deal with bugs raised by users who try to misuse the API. See the C# design guidelines at http://msdn.microsoft.com/en-us/library/k2604h5s(VS.71).aspx
Stu Mackellar
Yes, in the specific case of designing a library for external distribution, it makes sense to have a paranoid interface for whatever you're exposing. Even so, if the semantics of Foos property is sequential access, use ReadOnlyCollection and then return IEnumerable. No need to use wrong semantics ;)
Vojislav Stojkovic
You don't need to do either. You can return a read-only iterator without copying...
Jon Skeet
You don't need reflection to get back the underlying writable collection from a ReadOnlyCollection. Only (readCollection.GetEnumerator() as ICollection<Foo>).
shojtsy
+4  A: 

If you only need to iterate through the collection:

foreach (Foo f in bar.Foos)

then returning IEnumerable is enough.

If you need random access to items:

Foo f = bar.Foos[17];

then wrap it in ReadOnlyCollection.

Vojislav Stojkovic
+9  A: 

If you're using .NET 3.5, you can avoid making a copy and avoid the simple cast by using a simple call to Skip:

public IEnumerable<Foo> Foos {
    get { return foos.Skip(0); }
}

(There are plenty of other options for wrapping trivially - the nice thing about Skip over Select/Where is that there's no delegate to execute pointlessly for each iteration.)

If you're not using .NET 3.5 you can write a very simple wrapper to do the same thing:

public static IEnumerable<T> Wrapper<T>(IEnumerable<T> source)
{
    foreach (T element in source)
    {
        yield return element;
    }
}
Jon Skeet
Well there you go - 5 minutes on SO and I already feel a bit smarter :P
sirrocco
This answer just let me perform one of the best simplifications of code I've ever written. Thank you.
Chris Marasti-Georg
Note that there is a performance penalty for this: AFAIK the Enumerable.Count method is optimized for Collections casted into IEnumerables, but not for a ranges produced by Skip(0)
shojtsy
If wrapping a List in a class, would it be better to do the above or have the wrapping class implement IEnumerable<T> and pass on GetEnumerator()?
Andrew Robinson
@Andrew: It depends on the exact situation. Do you want your wrapping class act as the collection itself, or merely *contain* a collection?
Jon Skeet
@Jon, good question. I guess depending on which way one wanted to answer this either approach could be good. In my case, I think it is the collection itself so assuming passing on GetEnumerator() is a valid approach, then that is the way I would go. Thanks,
Andrew Robinson
A: 

Sometimes you may want to use an interface, perhaps because you want to mock the collection during unit testing. Please see my blog entry for adding your own interface to ReadonlyCollection by using an adapter.

+1  A: 

I avoid using ReadOnlyCollection as much as possible, it is actually considerably slower than just using a normal List. See this example:

List<int> intList = new List<int>();
        //Use a ReadOnlyCollection around the List
        System.Collections.ObjectModel.ReadOnlyCollection<int> mValue = new System.Collections.ObjectModel.ReadOnlyCollection<int>(intList);

        for (int i = 0; i < 100000000; i++)
        {
            intList.Add(i);
        }
        long result = 0;

        //Use normal foreach on the ReadOnlyCollection
        TimeSpan lStart = new TimeSpan(System.DateTime.Now.Ticks);
        foreach (int i in mValue)
            result += i;
        TimeSpan lEnd = new TimeSpan(System.DateTime.Now.Ticks);
        MessageBox.Show("Speed(ms): " + (lEnd.TotalMilliseconds - lStart.TotalMilliseconds).ToString());
        MessageBox.Show("Result: " + result.ToString());

        //use <list>.ForEach
        lStart = new TimeSpan(System.DateTime.Now.Ticks);
        result = 0;
        intList.ForEach(delegate(int i) { result += i; });
        lEnd = new TimeSpan(System.DateTime.Now.Ticks);
        MessageBox.Show("Speed(ms): " + (lEnd.TotalMilliseconds - lStart.TotalMilliseconds).ToString());
        MessageBox.Show("Result: " + result.ToString());
James Madison