3

The language I use is C#.

Say that a method of an object returns a List<int>. If we assign this result to a variable of type IEnumerable<int> would we benefit anything or not?

In other words

IEnumerable<int> result = list

is better than

List<int> result = list,

where list is a List<int>.

I think intuitively that we will not benefit anything. However, i would like to be sure. Hence I am asking this.

Gabe
  • 84,912
  • 12
  • 139
  • 238
Christos
  • 53,228
  • 8
  • 76
  • 108
  • 2
    List has functionality that IEnumerable does not. If you need this functionality, return List. Otherwise, return IEnumerable such that in future if any changes are done, it won't be a chain reaction of refactoring. – bland Dec 03 '13 at 17:44
  • Yes, but return IList instead of List. – fsimonazzi Dec 03 '13 at 17:51
  • @bland, so the only benefit I will have it will letter if I do a refactor of my existing code. In terms of optimization, I will not gain anything, If I got you. Right? – Christos Dec 03 '13 at 17:52
  • 1
    Correct, there is no optimization in this context. – bland Dec 03 '13 at 18:22

3 Answers3

1

The drawback is that you won't be able to use List<T> specific methods. The benefit is that you will be able to change the implementation in the method that returns the list to another kind of list (or set or ...) because the only thing you require is IEnumerable<T>.

A compromise is IList<T> if you need methods like Add.

And also, it is not a good idea to return List<T> from a method. If you are the implementer of the method, then you should at least return IList<T> or maybe even IEnumerable<T>.

In general there are circumstances where you have to cast to a base type, see new keyword in method signature.

Community
  • 1
  • 1
Lasse Espeholt
  • 17,622
  • 5
  • 63
  • 99
  • no I don't want any methods like Add. The only thing I want in my case is to use a foreach statement to go over all elements of the list. – Christos Dec 03 '13 at 17:46
  • @ChristosPaisios Then I see no reason why you shouldn't just use `IEnumerable`. Another option is to use `var`, but only do that if it is clear what type the method returns. – Lasse Espeholt Dec 03 '13 at 17:47
0

The only reason you would do this, is to return your list to a caller, in such a way, that the caller wouldn't be able to modify the list*.

*unless the caller performed a hard cast on the list

That said - it's always beneficial to use the least specific class/interface possible - as it opens up more possibilities for code reuse.

Dave Bish
  • 19,263
  • 7
  • 46
  • 63
0

In this specific case there is no reason to type the local variable as the interface. If the object implemented some interface explicitly, then you may want to do this, but that's not the case for List and IEnumerable. It implements IEnumerable<T> explicitly.

If you consider it likely that you'll be wanting to set something else to result that is an IEnumerable and not a List then you could do this, but generally I wouldn't bother to make such a change unless you're actually in that situation, rather than trying to do it preemtively. Being a local variable, rather than say part of the public API of a type, you'd always be able to change the type of the variable in a future revision without any real significant consequences.

Servy
  • 202,030
  • 26
  • 332
  • 449