关于代表的问题

Far*_*ine 1 c# delegates

可能重复:
C#:'+ = anEvent'和'+ = new EventHandler(anEvent)'之间的区别

让我们有这个代表:

delegate int Process (int x ,int y) ; 
Run Code Online (Sandbox Code Playgroud)

而这个方法:

int Add (int x , int y)
{
    return x+y ; 
}
Run Code Online (Sandbox Code Playgroud)

我的问题:

有什么区别:

Process MyProcess = Add ; 
Run Code Online (Sandbox Code Playgroud)

并且:

Process MyProcess = new Process (Add) ; 
Run Code Online (Sandbox Code Playgroud)

Mar*_*ers 6

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

C#2.0添加了隐式方法组转换,允许您编写第一个版本.两者是等价的.有时,在存在歧义的情况下,有必要使用更明确的形式.

有关详细信息,请参阅Jon Skeet的文章委派更改中的委托推断部分.