委托的例子

 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);
         }
     }   
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值