委托/ lambda打字和强制如何工作?

jny*_*len 9 c# lambda delegates type-inference anonymous-methods

我已经注意到一些在C#中处理lambda函数和匿名委托时工作和不起作用的事例.这里发生了什么?

class Test : Control {
    void testInvoke() {
        // The best overloaded method match for 'Invoke' has some invalid arguments
        Invoke(doSomething);

        // Cannot convert anonymous method to type 'System.Delegate' because it is not a delegate type
        Invoke(delegate { doSomething(); });

        // OK
        Invoke((Action)doSomething);

        // OK
        Invoke((Action)delegate { doSomething(); });

        // Cannot convert lambda expression to type 'System.Delegate' because it is not a delegate type
        Invoke(() => doSomething());

        // OK
        Invoke((Action)(() => doSomething()));
    }

    void testQueueUserWorkItem() {
        // The best overloaded method match for 'QueueUserWorkItem' has some invalid arguments
        ThreadPool.QueueUserWorkItem(doSomething);

        // OK
        ThreadPool.QueueUserWorkItem(delegate { doSomething(); });

        // The best overloaded method match for 'QueueUserWorkItem' has some invalid arguments
        ThreadPool.QueueUserWorkItem((Action)doSomething);

        // No overload for 'doSomething' matches delegate 'WaitCallback'
        ThreadPool.QueueUserWorkItem((WaitCallback)doSomething);

        // OK
        ThreadPool.QueueUserWorkItem((WaitCallback)delegate { doSomething(); });

        // Delegate 'WaitCallback' does not take '0' arguments
        ThreadPool.QueueUserWorkItem(() => doSomething());

        // OK
        ThreadPool.QueueUserWorkItem(state => doSomething());
    }

    void doSomething() {
        // ...
    }
}
Run Code Online (Sandbox Code Playgroud)

那是很多例子.我想我的问题如下:

  1. 为什么Invoke总是拒绝lambda函数或匿名委托,但是ThreadPool.QueueUserWorkItem没关系呢?

  2. 到底是什么"无法转换匿名方法来键入'System.Delegate'因为它不是委托类型"意味着什么?

  3. 为什么ThreadPool.QueueUserWorkItem接受没有参数的匿名委托,而不接受没有参数的lambda表达式?

Jon*_*eet 9

  1. ThreadPool.QueueUserWorkItem签名中有一个特定的代表; 调用只是有Delegate.Lambda表达式和匿名方法只能转换为特定的委托类型.

  2. 这只是一个错误的错误消息.这意味着,"我不确切地知道您要转换为哪种委托类型."

  3. 你正在使用一个没有参数列表的匿名方法,它可以转换为任何不使用out/ref参数的委托类型.如果您尝试过delegate() { ... }(即显式空参数列表),那么它将无效.这种"我不关心参数"的匿名方法能力是他们唯一具有lambda表达式不具备的功能.

在简单的任务,IMO的背景下最简单地展示所有这些:

// Doesn't work: no specific type
Delegate d = () => Console.WriteLine("Bang");

// Fine: we know the exact type to convert to
Action a = () => Console.WriteLine("Yay");

// Doesn't work: EventHandler isn't parameterless; we've specified 0 parameters
EventHandler e1 = () => Console.WriteLine("Bang");
EventHandler e2 = delegate() { Console.WriteLine("Bang again"); };

// Works: we don't care about parameter lists
EventHandler e = delegate { Console.WriteLine("Lambdas can't do this"); };
Run Code Online (Sandbox Code Playgroud)

  • 我以为我理解代表和lambdas.我是如此愚蠢. (4认同)