Are there any benefits to using a C# method group if available?

前端 未结 7 1131
孤街浪徒
孤街浪徒 2020-12-03 10:55

When dealing with something like a List you can write the following:

list.ForEach(x => Console.WriteLine(x));

7条回答
  •  悲&欢浪女
    2020-12-03 11:29

    As others have noted, there is an extra unnecessary layer of indirection induced by the lambda. However, there are subtle language differences as well. For example, in C# 3 generic type inference works differently on M(F) than on M(x=>F(x)) when attempting to perform return type inference.

    For details see:

    http://blogs.msdn.com/b/ericlippert/archive/2007/11/05/c-3-0-return-type-inference-does-not-work-on-member-groups.aspx

    and the follow-up:

    http://blogs.msdn.com/b/ericlippert/archive/2008/05/28/method-type-inference-changes-part-zero.aspx

提交回复
热议问题