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
  • 14,163
  • 30
  • 110
  • 141

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
  • 104,400
  • 10
  • 158
  • 276