3

Is it possible to rewrite the following code in a single statement?

foreach(var myVar in varEnumerable)
{
    MyMethod(myVar);
    Thread.Sleep(2000);
}

Without the Thread.Sleep(), I would've written it as:

varEnumerable.ToList().ForEach(x => MyMethod(x));
reggaemahn
  • 6,272
  • 6
  • 34
  • 59
  • 5
    FYI, ForEach isn't a part of LINQ, it's a method on List. –  Jan 07 '16 at 15:02
  • So no, it's not possible to rewrite it in a LINQ statement :) – D Stanley Jan 07 '16 at 15:03
  • 1
    @AndyJ Yes, thanks. Noted and updated. – reggaemahn Jan 07 '16 at 15:04
  • 3
    as AndyJ commented, `ForEach` is a common method belonging to `List` (not one of Enumerable extension methods). so you make a list first (memory waste). The second thing here is that lambda expressions looks very cryptic in stack trace (e.g. when you study exception case). So what is a profit to use `ForEach()`? `foreach` is absolutely adequate here – ASh Jan 07 '16 at 15:10
  • @ASh It's just a personal preference. But other than that i wouldn't call converting to list a memory waste. It's only an O(n) operation so performance hit would only be a concern for huge (read hundreds of thousands) collections. It is also useful if you don't want to lazy load because by the time the iteration happens, the original collection might have been modified, which I'm not looking for here. Correct me if I'm wrong. – reggaemahn Jan 07 '16 at 15:16
  • Downvote? How may I improve the question? – reggaemahn Jan 07 '16 at 15:21

2 Answers2

11

Yup, you just need some curly braces (and whitespace for good measure):

varEnumerable.ToList().ForEach(x => {
    MyMethod(x);
    Thread.Sleep(2000);
});

Edit: it's been noted in the comments on the question that this is less efficient than a plain foreach (due to the ToList() call), and by the time you add the braces and whitespace it doesn't look any cleaner, so it's not really a big win. So this is how you can do what you asked for, but it's probably not what you should do :)

adv12
  • 8,443
  • 2
  • 24
  • 48
  • And this is called "anonymous method". Just for OP's knowledge – Alex Jan 07 '16 at 15:02
  • 1
    @Alex That's a lambda, rather than an Anon Method. https://msdn.microsoft.com/en-GB/library/0yw3tz5k.aspx –  Jan 07 '16 at 15:11
  • @AndyJ There'a a [fine line](http://stackoverflow.com/a/299712/2025666) between the two. – reggaemahn Jan 07 '16 at 15:30
  • @Jeevan Jose I'd say named methods and anon methods are closer than lambdas and anon methods. Lambdas can be converted in to delegates sure, but they can be used in other ways such as creating expression trees as David B notes in your link. The "fine line" here is that they all end up as delegates in this situation, but in other situations lambdas can be very different beasts. –  Jan 07 '16 at 15:49
4

What about:

varEnumerable.ToList().ForEach(x => {MyMethod(x);Thread.Sleep(2000);});

You can group multiple commands using accolades ({}) and semicolons (;).

Note that you can use any kind of instructions in such environment (for loops, instructions,...)

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555