c# - 关于委托(delegate)的问题

标签 c# delegates

<分区>

Possible Duplicate:
C#: Difference between ‘ += anEvent’ and ‘ += new EventHandler(anEvent)’

让我们有这个委托(delegate):

delegate int Process (int x ,int y) ; 

和这个方法:

int Add (int x , int y)
{
    return x+y ; 
}

我的问题:

有什么区别:

Process MyProcess = Add ; 

和:

Process MyProcess = new Process (Add) ; 

最佳答案

在 C# 1.x 中,只有第二个版本可以编译。

添加了 C# 2.0 implicit method group conversions这使您可以编写第一个版本。两者是等价的。有时在有歧义的情况下需要使用更明确的形式。

参见 Delegate inference 部分来自 Jon Skeet 的文章 Delegate changes获取更多信息。

关于c# - 关于委托(delegate)的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3418321/

相关文章:

c# - 在 C# 中开发 winform 应用程序是否有任何标准的屏幕分辨率

c# - 委托(delegate)在 c# 中不可变的目的是什么?

ios - 如何为 URLSessionDataTask 分配委托(delegate)?

c# - 如何在 C# 中按类型创建动态委托(delegate)对象?

ios - 根据 TableView 选择将功能分配给第二个 View Controller 中的 IOS 按钮

c# - 使用 OWIN 的网络套接字

字符串中带有大括号的 C# String.Format

ios - 自定义委托(delegate)在 iOS 中不起作用

c# - 在 C# 中存储查询还是在 Postgres 中使用存储函数?

c# - 多个 AddParameter 调用只添加第一个参数,忽略其余参数