1

Please suppose I have the following extension method in order to be able to force evaluation of an IEnumerable:

public static List<T> EvaluateNow<T>(this IEnumerable<T> collection)
{
    if (collection == null)
    {
        throw new ArgumentNullException("collection");
    }
    else
    {
        return (collection.ToList());
    }
}

I would like to ask if there is any point in having IList<T> instead of List<T> as this method's return type (i.e., downcasting to an interface) in order to tie neither it nor its dependencies to a particular implementation (even if the return value will always actually be a List).

DotNetStudent
  • 889
  • 9
  • 24
  • 4
    For this particular function I don't think so. Also I don't think its worth having a extension method like this that just runs `.ToList()` – Magnus Oct 01 '11 at 13:16
  • 1
    IList allows one of many implementations, one being List. If that's not important to support future change in structures, then no. – kenny Oct 01 '11 at 13:18
  • However ity is a good habit to get into. One day you WILL need to support future change that you did not anticipate ;) – Aaron Gage Oct 23 '11 at 03:50

2 Answers2

3

It is always a good idea to use interfaces in situations like this rather than classes.
The reasons why are too numerous to get into here, but for example, it greatly increass flexibility as any class just needs to implement the interface IList<T> to be assignable to the return value of this function. Classes will implement this (or other) interfaces without subclassing List (or equivalent).
In general you want to use the 'lowest' heirachical interface that fits the requirements. In this case, you could consider ICollection, or even IEnumerable instead of IList.

It is irrelevant what class instance you return from the function, as only 'the interface' is returned, so it can be assigned to any class implementing that interface (again, max flexibilty?)

Often a function returning some IEnumerable object will mostly be used inside a foreach loop.

Aaron Gage
  • 2,373
  • 1
  • 16
  • 15
  • 1
    Well, Microsoft apparently decided that `ToList()` should return `List` rather than `IList` – Magnus Oct 01 '11 at 13:43
1

An extension method can only be called on an instance. Which means that collection can never be null in your method.

So... as written your method has the exact same behavior the extension method IEnumerable.ToList so I don't see any point.

It would be useful if its return type was IList as Aaron explains in his answer.

Why does IEnumerable.ToList not already return a IList would be a good question for the library designers.

Community
  • 1
  • 1
Miserable Variable
  • 28,432
  • 15
  • 72
  • 133
  • Have you ever tried `IList newList = null; int newInt = newList.First();`? The compiler won't complain when you apply the extension method `First()` (defined on `System.Linq`), but when you run the program you will certainly get an exception, so I would say that extension methods can indeed be called on `null` references. – DotNetStudent Oct 01 '11 at 13:33
  • 1
    @DotNetStudent indeed I stand corrected. But the behavior would still be same..throw `ArgumentNullException`. I will modify my answer. – Miserable Variable Oct 01 '11 at 13:38
  • 1
    But `ToList()` internally throws the same exception as your method does, which means that just like @Hemal says you are just creating an alias for `ToList()` – Magnus Oct 01 '11 at 13:41
  • 1
    @DotNetStudent...when newList.First() throws exception it is not from inside the `First` method but at the call-site. Nevertheless, I think you helped improved my answer. – Miserable Variable Oct 01 '11 at 14:02