`

委托的例子

阅读更多

using System;
using System.Collections.Generic;
using System.Text;

namespace EventEventHandlerTest
{
public delegate void MyDelegate(string str);

public class Program
{
public static void Main(string[] args)
{
MyDelegate d1 = new MyDelegate(C.M1);
d1("D1");
MyDelegate d2 = new MyDelegate(C.M2);
d2("D2");
MyDelegate d3 = new MyDelegate(new C().M3);
d3("D3");
MyDelegate d4 = new MyDelegate(d3);
d4("D4");
Console.WriteLine("---------------");
MyDelegate d5 = d1 + d2;
d5("D5");
Console.WriteLine("---------------");
d5 += d3;
Console.WriteLine("---------------");
d5("D55");
Console.WriteLine("---------------");
MyDelegate d6 = d5 - d3;
d6("D6");
Console.WriteLine("---------------");
d6 -= d6;
d6("D6");
Console.WriteLine("---------------");
Console.WriteLine("---------------");
Console.ReadLine();
}
}

public class C
{
public static void M1(string str)
{
Console.WriteLine("From:C.M1: {0}", str);
}

public static void M2(string str)
{
Console.WriteLine("From:C.M2: {0}", str);
}

public void M3(string str)
{
Console.WriteLine("From:C.M3: {0}", str);
}
}

public class C1
{
public static void P1(string str)
{
Console.WriteLine("From:C1.P1: {0}", str);
}
}

public class C2
{
public void P1(string str)
{
Console.WriteLine("From:C2.P1: {0}", str);
}
}
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics