C#命名方法委托的基础引用
示例
在将命名方法分配给委托时,如果满足以下条件,则它们将引用相同的基础对象:
它们是相同的实例方法,在类的相同实例上
它们是类上的相同静态方法
publicclassGreeter
{
publicvoidWriteInstance()
{
Console.WriteLine("Instance");
}
publicstaticvoidWriteStatic()
{
Console.WriteLine("Static");
}
}
//...
Greetergreeter1=newGreeter();
Greetergreeter2=newGreeter();
Actioninstance1=greeter1.WriteInstance;
Actioninstance2=greeter2.WriteInstance;
Actioninstance1Again=greeter1.WriteInstance;
Console.WriteLine(instance1.Equals(instance2));//假
Console.WriteLine(instance1.Equals(instance1Again));//真正
Action@static=Greeter.WriteStatic;
ActionstaticAgain=Greeter.WriteStatic;
Console.WriteLine(@static.Equals(staticAgain));//真正