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