tags:

views:

62

answers:

5

I see some people write:

//wordList is List<string> 
wordList.ForEach(delegate(string word){ Console.WriteLine(word);});

instead of:

foreach(string word in wordList)
{
    Console.WriteLine(word);
}

What is the advantage in doing so. Also I couldn't fathom the Action delegate syntax given above though I have used delegates in C# 2.0. Basically I am not able to relate the syntax with the concept of delegates I am familiar with. Can you please help me understand the syntax. Is it some shorthand?

+2  A: 

Eric Lippert has blogged about exactly this topic.

You should definitely read the article, but to briefly summarize one of his key points, he suggests that: expressions whose sole purpose is to produce side-effects should be statements. The premise here is that creating a functional extension for a foreach loop adds little expressive power to the language, and can be harmful in some cases.

Quoting from his article:

When we provide two subtly different ways to do exactly the same thing, we produce confusion in the industry, we make it harder for people to read each other’s code, and so on. Sometimes the benefit added by having two different textual representations for one operation (like query expressions versus their underlying method call form, or + versus String.Concat) is so huge that it’s worth the potential confusion. But the compelling benefit of query expressions is their readability; this new form of “foreach” is certainly no more readable than the “normal” form and is arguably worse.

LBushkin
+2  A: 

The delegate() {...} syntax is available in .NET 2. It essentially creates an (anonymous) method in the containing class that has the contents of the delegate. So, the syntax above is equivalent to

private void actionImpl(string word) {
    Console.WriteLine(word);
}

wordList.ForEach(new Action<string>(actionImpl));

Regarding List<T>.ForEach, Eric Lippert has blogged about this. It's useful for when an action has no sideeffects & is a very simple one liner. Using implicit delegate creation & method groups, you could reduce your code example to

wordList.ForEach(Console.WriteLine);

as Console.WriteLine(string s) matches the Action<string> delegate signature. This is less code and (I would argue) clearer than using an explicit foreach loop, in this particular instance.

thecoop
A: 

The delegate syntax is the use of an anonymous function.

It allows you to define the function inline to help clarify your code. You can find a more detailed explanation on msdn: http://msdn.microsoft.com/en-us/library/bb882516.aspx

Darien Ford
A: 

The first is more of a functional style of programming. It leaves the implementation details of how each word is printed up to the underlying system. Although probably not in your example, it does leave the system up making optimizations (parallelization being a key one) about the implementation details where the second does not.

In the first example, you are telling the system "Do this to each item." In the second example you are saying "Loop through this collection and do this."

The book Real World Functional Programming talks about the difference between the two styles.

Kevin
A: 

If you're not passing Action<string> instances around between methods, there's no advantage to the delegate syntax.

David B