When dealing with something like a List<string> you can write the following:
list.ForEach(x => Console.WriteLine(x));
or you can use a method group to do the same operation:
list.ForEach(Console.WriteLine);
I prefer the second line of code because it looks cleaner to me, but are there any benefits to this?
Well, let’s take a look and see what happens.
This gets compiled into the following IL.
Notice how the method group approach creates an
Action<T>delegate for one time use and the lambda expression approach creates a hidden anonymous delegate field and does an inline initialization of it if necessary. Noticebrtrueinstruction atIL_000a.