Using an anonymous method

I did not use anonymous methods. I found code that repeats the list, as shown in code snippet 1. Why is code snippet 1 preferable to 2?

List<String> names = new List<String>(); ... //Code snippet 1 names.ForEach(delegate(String name) { Console.WriteLine(name); }); //Code snippet 2 foreach (string name in names) { Console.WriteLine(name); } 
+4
source share
2 answers

I do not see fragment 1 use a lot. I see a variation using lambda expressions.

 names.ForEach(x=> Console.WriteLine(x)); 
+7
source

In this case, there is no use.

You could find older programmers using method 2 in your example, and newer programmers can use method 1.

Older programmers have more experience with anonymous methods, anonymous methods are new, not โ€œrooted in their souls,โ€ and they are automatically programmed in style # 2.

New programmers can use # 1 because they continue to think that all this is a method call.

0
source

All Articles