Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which do you prefer for interfaces: T[], IEnumerable<T>, IList<T>, or other?

Ok, I'm hoping the community at large will aid us in solving a workplace debate that has been ongoing for a while. This has to do with defining interfaces that either accept or return lists of some type. There are several ways of doing this:

public interface Foo
{
    Bar[] Bars { get; }
    IEnumerable<Bar> Bars { get; }
    ICollection<Bar> Bars { get; }
    IList<Bar> Bars { get; }
}

My own preference is to use IEnumerable for arguments and arrays for return values:

public interface Foo
{
    void Do(IEnumerable<Bar> bars);
    Bar[] Bars { get; }
}

My argument for this approach is that the implementation class can create a List directly from the IEnumerable and simply return it with List.ToArray(). However some believe that IList should be returned instead of an array. The problem I have here is that now your required again to copy it with a ReadOnlyCollection before returning. The option of returning IEnumerable seems troublesome for client code?

What do you use/prefer? (especially with regards to libraries that will be used by other developers outside your organization)

like image 894
csharptest.net Avatar asked Sep 21 '09 18:09

csharptest.net


People also ask

Should I use IList or IEnumerable?

You use IEnumerable when you want to loop through the items in a collection. IList is when you want to add, remove, and access the list contents out of order. Save this answer.

Should I return IEnumerable or ICollection?

If you do not counting in your external code it is always better to return IEnumerable, because later you can change your implementation (without external code impact), for example, for yield iterator logic and conserve memory resources (very good language feature by the way).

What is the use of IEnumerable ICollection IList and IDictionary?

To begin with, all the interfaces (ICollection, IList, IQueryable, IDictionary) inherit from IEnumerable, which allows us to use the foreach statement. ICollection: is the most basic interface among these, it is enumerable and it supports Count operator. IQueryable: an enumerable interface that supports LINQ.

Should I use IEnumerable?

IEnumerable is best to query data from in-memory collections like List, Array etc. IEnumerable doesn't support add or remove items from the list. Using IEnumerable we can find out the no of elements in the collection after iterating the collection. IEnumerable supports deferred execution.


2 Answers

My preference is IEnumerable<T>. Any other of the suggested interfaces gives the appearance of allowing the consumer to modify the underlying collection. This is almost certainly not what you want to do as it's allowing consumers to silently modify an internal collection.

Another good one IMHO, is ReadOnlyCollection<T>. It allows for all of the fun .Count and Indexer properties and unambiguously says to the consumer "you cannot modify my data".

like image 165
JaredPar Avatar answered Oct 30 '22 07:10

JaredPar


I don't return arrays - they really are a terrible return type to use when creating an API - if you truly need a mutable sequence use the IList<T> or ICollection<T> interface or return a concrete Collection<T> instead.

Also I would suggest that you read Arrays considered somewhat harmful by Eric Lippert:

I got a moral question from an author of programming language textbooks the other day requesting my opinions on whether or not beginner programmers should be taught how to use arrays.

Rather than actually answer that question, I gave him a long list of my opinions about arrays, how I use arrays, how we expect arrays to be used in the future, and so on. This gets a bit long, but like Pascal, I didn't have time to make it shorter.

Let me start by saying when you definitely should not use arrays, and then wax more philosophical about the future of modern programming and the role of the array in the coming world.

like image 24
Andrew Hare Avatar answered Oct 30 '22 06:10

Andrew Hare