I have a method:
static void FileChangesDetected(List<ChangedFiles> files)
I used Visual Studio 2010 and Resharper. Resharper always recommends that I change the List<T> to IEnumerable<T>, and I’m wondering why this is.
In the method, I simply do this:
foreach (var file in files)
{ ... }
Is there a benefit of using IEnumerable<T> rather than List<T>?
It all has to do with LSP (Liskov substitution principle).
Basically, instead of using implementations, it is better to code to abstractions.
In this specific case, if all you do is loop over the list, you can use
IEnumerable<T>as the simplest abstraction – this way you don’t have to use aList<T>, but any collection type in your function.This allows your functions to be more reusable and reduces coupling.