1
为什么copyOfDelegate是原始代理的副本,而不是原始副本的副本?为什么.net委托赋值运算符不会将引用分配给原始委托?
public class DelegateTester
{
public delegate void PrintDelegate();
public PrintDelegate PrintCallback;
}
public class Client
{
public void Print()
{
Console.WriteLine("in client");
}
}
static void main()
{
DelegateTester tester = new DelegateTester();
Client client = new Client();
tester.PrintCallback += new DelegateTester.PrintDelegate(client.Print);
tester.PrintCallback += new DelegateTester.PrintDelegate(client.Print);
// copy the delegate
DelegateTester.PrintDelegate copyOfDelegate = tester.PrintCallback;
tester.PrintCallback -= new DelegateTester.PrintDelegate(client.Print);
tester.PrintCallback();
copyOfDelegate.Invoke();
}