9

If I'm explaining the following ForEach feature to someone, is it accurate to say that #2 is the "LINQ foreach approach" or is it simply a "List<T> extension method" that is not officially associated with LINQ?

var youngCustomers = from c in customers
                     where c.Age < 30
                     select c;

//1. traditional foreach approach
foreach (var c in youngCustomers)
{
    Console.WriteLine(c.Display());
}

//2. LINQ foreach approach?
youngCustomers.ToList().ForEach(c => Console.WriteLine(c.Display()));
Edward Tanguay
  • 189,012
  • 314
  • 712
  • 1,047
  • 3
    Check this post from Eric Lippert http://blogs.msdn.com/b/ericlippert/archive/2009/05/18/foreach-vs-foreach.aspx – Brian Rasmussen Jul 19 '10 at 13:34
  • 1
    It's worth noting that your #2 will loop through the items twice - once to create the list, and once for the ForEach method. If you want to use that style of code, which is perfectly legitimate, you're better off writing your own ForEach extension method on IEnumerable. – Joel Mueller Jul 19 '10 at 16:39

6 Answers6

24

it is a normal method of List<T> though people often provide their own extension methods for other IEnumerable<T>. LINQ does not provide a ForEach extension due to its design goals of being functional/ working with immutable types, ForEach is an inherently side effect/imperative operation.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
jk.
  • 13,817
  • 5
  • 37
  • 50
  • this is what I suspected but was unsure since if you do a google search for "linq foreach" it seems to be widely held misconception that .ForEach() is a feature of LINQ – Edward Tanguay Jul 19 '10 at 13:25
10

It has nothing to do with LINQ and it's not an extension method.

ForEach is a plain instance method on the List<T> class.

(And if you wanted to be really nitpicky, then ForEach is not part of the C# language at all: it's part of the .NET Base Class Library.)

LukeH
  • 263,068
  • 57
  • 365
  • 409
  • @abatishchev: My understanding is that the BCL and FCL aren't synonymous. The FCL is a superset of the BCL. See http://en.wikipedia.org/wiki/Base_Class_Library and http://en.wikipedia.org/wiki/Framework_Class_Library. – LukeH Jul 19 '10 at 13:59
  • Yea, I saw them both. I think this two articles are somewhat indistinct. I don't know why Microsoft didn't edit them yet. As it follows from the articles, BCL contains only ECMA standardized namespaces meanwhile FCL - all standard namespaces. Right? – abatishchev Jul 20 '10 at 06:43
  • @abatischev: I'm not really sure and, as you say, there doesn't really seem to be any official information available. My interpretation is that the BCL is a superset of the ECMA standard libs, and then the FCL is a superset of the BCL. – LukeH Jul 20 '10 at 09:06
2

It is not an extension method of List<T>, it is a regular method of List<T>.

So it has nothing to do with LINQ. If it had, the distinction between "officially associated with LINQ" and not officially associated with LINQ is not practically a very useful one. LINQ is simply a bunch of chained extension methods (often on IEnumerable<T>). There is rarely any point in distinguishing it from other extension methods on IEnumerable<T>. The best distinction would be that they reside in one of the System.Linq or System.Something.Linq namespaces.

David Hedlund
  • 128,221
  • 31
  • 203
  • 222
2

It's not LINQ. One of the design aspects of LINQ is that the standard LINQ methods do not have side-effects. The ForEach method would violate this.

Eric Lippert has a blog article all about this:

Link

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
spender
  • 117,338
  • 33
  • 229
  • 351
1
youngCustomers.ToList().ForEach(c => Console.WriteLine(c.Display())); 

Let's break it down:

  • ToList is a call to System.Linq.Enumerable.ToList() introduced in .net framework 3.5
  • ForEach is a call to System.Collections.Generic.List<T>.ForEach introduced in .net framework 2.0
  • c => is lambda expression syntax introduced in c# 3.0.

If you look at the documentation for List<T>.ForEach, you can see the old delegate syntax that was required back then to call it.

Amy B
  • 108,202
  • 21
  • 135
  • 185
0

Side-effecting foreach will be ( is ) provided in Visual Studio 2010's Linq extensions.

Mike
  • 1