Why no ForEach statement?

M

Michael C

Linq defines all sorts of useful extension methods but as far as I can see
it doesn't have a "do something to every item in a collection", eg to add 1
to SomeProperty for every item in my collection:

MyCollection.ForEach(i => i.SomeProperty += 1);

The array has a ForEach method but nothing else does. I've defined my own
but am wondering if I'm missing something that is already existing.

Cheers,
Michael


public static void ForEach<T>(this IEnumerable<T> Items, Action<T>
action)
{
foreach (T item in Items)
{
action(item);
}
}

public static void ForEach(this IEnumerable Items, Action<object>
action)
{
foreach (object item in Items)
{
action(item);
}
}
 
M

Michael C

Peter Duniho said:
You aren't missing anything, and yes...I for one agree that not having a
ForEach() extension method already defined in the Enumerable class is an
odd omission.

That said, as you already know, it's trivial to add yourself if you really
want it, and I suppose that technically it adds very little conciseness to
the code, since C# already has a foreach() statement. And I admit, a
ForEach() method wouldn't necessarily actually fit in with the theme in
the LINQ Enumerable class, those almost exclusively being about
transforming an existing enumerable, or generating certain kinds of
enumerables.

So, _I_ would have put the ForEach() method in, but I can understand why
Microsoft decided against it.

Cool, thanks, that's what I needed to know. It's odd they put it on Array
and List but not as an extension method but I guess that's the great thing
about linq and extension methods, they're very easy to extend :)

Michael
 
R

RayLopez99

You aren't missing anything, and yes...I for one agree that not having a  
ForEach() extension method already defined in the Enumerable class is an  
odd omission.

Seems like it's the classic case of the blind leading the blind in
this thread.

Your compiler may be out of date, but in fact C#3 does support
'foreach' in LINQ.

An example given on p. 236 of Jon Skeet's book C# In Depth shows:

var films = new List <Film>
{...}

Action<Film> UserFunction = x => {//some function here};

films.ForEach(UserFunction);

Live and lern.

RL
 
A

Anthony Jones

You aren't missing anything, and yes...I for one agree that not having a
ForEach() extension method already defined in the Enumerable class is an
odd omission.
Seems like it's the classic case of the blind leading the blind in
this thread.
Hmm... I think that's going to make you sound very silly ....

<<<<<<<<<
Your compiler may be out of date, but in fact C#3 does support
'foreach' in LINQ.

An example given on p. 236 of Jon Skeet's book C# In Depth shows:

var films = new List <Film>
{...}

Action<Film> UserFunction = x => {//some function here};

films.ForEach(UserFunction);

Live and lern.
<<<<<<<<

The ForEach to which you refer is actually provide by List<T> not as an
extension method in LINQ on IEnumerable<T> which is what the OP is
lamenting. Most other collection types do not have a ForEach method.
 
M

Michael C

RayLopez99 said:
Seems like it's the classic case of the blind leading the blind in
this thread.
Your compiler may be out of date, but in fact C#3 does support
'foreach' in LINQ.

I'm well aware of this and I'm sure peter is, this is where I got the idea
from. It's actually a method of Array and List but does not exist on
IEnumerable said:
Live and lern.

The lesson you need to learn here is to think before speaking. :) I
actually said in my first post that the Array has a ForEach method.

Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top