Why is it preferable to use IEnumerable <T> for list <T>

I heard that it is important to use the lowest class when passing parameters to methods. Why is this? Also where can I find more information on what a class hierarchy is? I would like to know what IEnumerable belongs to and so on.

+3
source share
5 answers

If you use IEnumerable<T>as a parameter type, then you can pass any type that implements this interface. This includes List<T>, Stack<T>, Queue<T>etc.

It also includes various anonymous types, which can be the result of a LINQ query, as well as a very important one IQueryable<T>.

+4
source

"low-level", . .

MSDN , ( IEnumerable , "" )

+3

IEnumerable - , .

API , IList<T> , , , , IEnumerable<T>, ReadOnlyCollection. , .Add/.Remove ..

, , IEnumerable, /, IList.

: List<T>, IList<T>. , List - , - , IList - , , .

+2

, , : , , List<T>?

, - LinkedList<T> SortedList<T>, , , , , .

, .

+1

The idea is to maximize the flexibility of your function. If you need a List <T>, then callers must have one to go through. If they do not have one convenient, they will have to create it, and it is expensive. If you require IEnumerable <T>, on the other hand, they can be passed to any collection.

The best place to learn about class heiarchy in .NET is MSDN.

+1
source

All Articles