While writing code similar to the following lines, ReSharper suggested the “Replace With Method Group” Refactoring for Line 2.
1: var names = Directory.GetFiles(@"c:\Program Files").ToList();
2: names.ForEach(n => Console.WriteLine(n));
The Method Group shorthand is usually found in places where you take the following code
myButton.Click += new EventHandler(myButton_Click);
and replace it with this.
myButton.Click += myButton_Click;
The C# compiler can infer the usage of the EventHandler class, giving you cleaner, less noisy code.
Letting Resharper apply the Method Group refactoring produces the following code.
1: var names = Directory.GetFiles(@"c:\ProgramFiles").ToList();
I was pleasantly surprised by the results. Apparently, I don’t have to explicitly declare the intermediate lambda variable ‘n’ just so I can pass it into the WriteLine method. The compiler is smart enough to infer this for me. Although a little confusing at first, this shorthand notation is pretty nice. I doubt I would have realized this without ReSharpers’ refactoring suggestions, one of the many reasons I’m a R# Junkie.