Why has a lambda with no capture changed from a static in C# 5 to an instance method in C# 6?

前端 未结 3 625
借酒劲吻你
借酒劲吻你 2020-12-14 06:12

This code throws an exception on the marked line:

using System;
using System.Linq.Expressions;

namespace ConsoleApplication2
{
    class Program
    {
              


        
3条回答
  •  臣服心动
    2020-12-14 07:02

    Roslyn (the C# compiler used by VS 2015) changed all lambda methods to non-static methods, whether they capture variables or not. See Delegate caching behavior changes in Roslyn. As I explain, this is an allowed behavior because anonymous methods (like those at issue here) that don't capture variables have fewer lifetime requirements than those that do. This doesn't mean, though, that those methods must be static: this is merely an implementation detail.

提交回复
热议问题