IEnumerable vs List – What to Use? How do they work?

Technology CommunityCategory: C#IEnumerable vs List – What to Use? How do they work?
VietMX Staff asked 3 years ago

IEnumerable describes behavior, while List is an implementation of that behavior. When you use IEnumerable, you give the compiler a chance to defer work until later, possibly optimizing along the way. If you use ToList() you force the compiler to reify the results right away.

Whenever you “stacking” LINQ expressions, you should use IEnumerable, because by only specifying the behavior gives LINQ a chance to defer evaluation and possibly optimize the program.