3

I want to write something like:

(0 .. 7).ForEach(i => SomeMethod(i))

but either I can't find the correct syntax, or this isn't possible in C#. If it can be done, what is the correct syntax please?

David Arno
  • 42,717
  • 16
  • 86
  • 131

4 Answers4

4

You can use Enumerable.Range Method

foreach (var i in Enumerable.Range(0, 7)) SomeMethod(i);

And if you add ForEach extension method as @Richard suggested, just do:

Enumerable.Range(0, 7).ForEach(SomeMethod);
ie.
  • 5,982
  • 1
  • 29
  • 44
  • Using method group is a great suggestion. I was wondering which solution I preferred, but now there's no question that I like better the extension method approach. – SimpleVar May 14 '12 at 09:07
  • I'm with Yorye: this answer's worth an upvote, but I prefer Richard's solution. – David Arno May 14 '12 at 09:11
  • agreed, I'm personally have ForEach extension method in my projects, but it is not in BCL, so I didn't suggested it at first. but I also upvoted Richard's solution – ie. May 14 '12 at 09:12
  • +1 I prefer this solution, this is not a lambda-happy solution. You can just merely pass the SomeMethod's address. Others, upon discovering lambda, will do it like this, a tautology: `Enumerable.Range(0, 7).ToList().ForEach(x => SomeMethod(x))` They are forgetting that it could be done simpler(just pass the method's address). You could prune that lambda and *just pass* the method's address instead `Enumerable.Range(0, 7).ToList().ForEach(SomeMethod)` – Michael Buen May 14 '12 at 09:22
4

To achieve what you're after, you'll need to add the illusive and well-known LINQ ForEach statement.

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

Usage:

Enumerable.Range(0, 7).ForEach(i => SomeMethod(i));

Enumerable.Range(0, 7).ForEach(i => Console.WriteLine(i.ToString());

Enumerable.Range(0, 7).ForEach(i => 
{
    string oddEven = i % 2 == 0 ? "even" : "odd";
    Console.WriteLine(string.Format("{0} is {1}", i, oddEven));
}

Extra reading

"foreach" vs "ForEach"

Richard
  • 8,110
  • 3
  • 36
  • 59
  • +1 Nice solution. Add usage example for becoming the perfect answer. – SimpleVar May 14 '12 at 09:02
  • I like this solution. It just seems odd to me that the framework doesn't already include this extension method. Guess I'd better read the "foreach" vs "ForEach" article to learn more. – David Arno May 14 '12 at 09:12
  • Here's the rationale why it is not included, and why it should not be included: http://blogs.msdn.com/b/ericlippert/archive/2009/05/18/foreach-vs-foreach.aspx – Michael Buen May 14 '12 at 09:19
  • @DavidArno: http://stackoverflow.com/questions/101265/why-is-there-not-a-foreach-extension-method-on-the-ienumerable-interface – Tim Schmelter May 14 '12 at 09:20
  • At the core of it all, the argument against it is "the purpose of an expression is to compute a value, not to cause a side effect. The purpose of a statement is to cause a side effect". With this in mind, I tend to only use the `ForEach` expression where the items won't be effected, but instead are used in a computation. – Richard May 14 '12 at 09:40
0

Enumerable.Range(0, 7).ToList().ForEach(i => whatever)

You need ToList, because pure IEnumerable doesn't have ForEach. (Well, you can easily define your own if you need one.)

Or maybe Enumerable.Range(0, 7 + 1), if you want including 7 as well.

Vlad
  • 35,022
  • 6
  • 77
  • 199
0

Have a look at Enumerable.Range();

ColWhi
  • 1,077
  • 6
  • 16