1

Everytime I've had to return a collection, I've returned a List. I've just read that I should return IEnumerable or similar interface (IQueryable for instance).

The problem I see is that often I want to work with a List. To do that, I'd have to do a .ToList() on the returned result.

Example

//...
List<Guid> listOfGuids = MyMethod().ToList();
//...
public IEnumerable<Guid> MyMethod()
{
    using (var context = AccesDataRépart.GetNewContextRépart())
    {
        return context.MyTable.ToList();
    }
}

Is executing a .ToList() twice the right practice.

8
  • Why do you call ToList in MyMethod? You could return context.MyTable directly... Nov 26, 2013 at 15:25
  • 3
    No @Thomas, the context will be disposed and MyTable will be unqueryable
    – Moho
    Nov 26, 2013 at 15:26
  • if you want to use the results as a list, simply return the list object. You could use IList<T> as the return type.
    – Moho
    Nov 26, 2013 at 15:28
  • @Moho, you're right, I didn't think about that Nov 26, 2013 at 15:29
  • 1
    Only return IEnumerable if all you want to do is iterate it, as that is the public contract of IEnumerable. IList contract includes adding and removing items, etc. There is no single piece of advice other than return just enough functionality to meet the required contract. Nov 26, 2013 at 15:29

5 Answers 5

3

If the caller actually needs a list, return a list (if that's what you have). Returning an IEnumerable when you already have a list, and when you know the caller is going to need a list, is just being wasteful, and for no real benefit.

If you feel that there is a chance that you'll be changing the underlying type of the object you are returning in future versions of the method it can, potentially, make it a bit easier on the library implementer to return an interface instead, but it's easier on the caller of the method when a more derived type is returned (they have the ability to do more with it than if they are just given an interface).

It is the reverse with input parameters. When passing parameters in the more derived the type the more "power" the library implementer has to work with the type, especially in future revisions, but using a much less restrictive type makes life easier on the caller of your library, as they don't need to convert what they have to what your method accepts.

This makes these decisions something to think about a fair bit when writing a libraries public API. You need to consider how much "power" you need right now, as well as how much you think you might need in the future. Once you know how restrictive/general the types need to be for you to do your job, you can then work to make your methods more convenient to use for callers. There is no one answers that will apply in every case. Saying that you should always return IEnumerable instead of List isn't proper, just the same as saying that you should always return List is also improper. You need to make a judgement call based on the specific situation you are in.

2

I would recommend just returning a List<T>, or perhaps an IList<T>. The reason that someone might recommend against returning List, is that it locks you in to that implementation. Depending on the usage of the API, that might not be a concern.

My general rule of thumb is to be more permissive in what you accept and more specific in what you return. So, IEnumerable<T> for method parameters, and IList<T>, List<T> or possibly even T[] for method return values.

1

You don't have to call ToList on the returned value, It is already a List. The reason you can't return IEnumerable is that you have using statement around your DataContext it will be disposed. So modify your method return type as List<T> and then don't call ToList on the returned value.

//...
List<Guid> listOfGuids = MyMethod(); //No ToList here
//...
public List<Guid> MyMethod()
{
    using (var context = AccesDataRépart.GetNewContextRépart())
    {
        return context.MyTable.ToList();
    }
}
1

I've just read that I should return IEnumerable or similar interface (IQueryable for instance).

Don't worry about that - return IList<> or List<> if you actually need a list object at the point the collection is consumed. The problem with returning IEnumerable can be that no-one knows what the cost of enumerating it is going to be - which is a down-side to the whole Linq concept that doesn't always get fair mention from the people who are encouraging everyone to return IEnumerable everywhere.

0

It really depends. Do you want to enumerate the collection before or after returning it?

Enumerate before: Every time you call ToList, ToArray, etc. you are enumerating the IEnumerable. If you are doing this many times after it is returned, this can be redundant and wasteful. Either returning it in an already enumerated form (e.g., IList, Array) or enumerating it once after returned and using that for the future processing probably be more preferable.

Enumerate after: Returning an IEnumerable allows you to defer the enumeration of the collection until later (e.g., save processing up front). If it turns out that you never end up enumerating the collection, or you only enumerate a subset of it, then the IEnumerable approach can be very advantageous.

1
  • He must materialize the sequence in this case because the context is being disposed of. This is not an option to consider here, the question is merely about the type of the return value, not whether or not to defer materializing the results of the query.
    – Servy
    Nov 26, 2013 at 15:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.