0

Why does LINQ .ForEach require a List object, while the foreach statement only requires IEnumerable?

I prefer using foreach when having multiple lines and LINQ for single line statements - just for readability.

But what is preferred, and why? And why the difference?

bytecode77
  • 13,209
  • 30
  • 105
  • 134

1 Answers1

4

There is no Linq ForEach() method. The method you are looking at is a member of List<T>.

See here for a discussion on why Linq/IEnumerable doesn't have ForEach().

Community
  • 1
  • 1
Matthew Watson
  • 96,889
  • 9
  • 144
  • 240