转载  一个多播委托 收藏

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace ConsoleApplication1
  5. {
  6.     class BroadCastDelegate
  7.     {
  8.         public static void Printf(string Msg)
  9.         {
  10.             Console.WriteLine(Msg);
  11.         }
  12.         public void Method1(string Msg)
  13.         {
  14.             Console.WriteLine(Msg);
  15.         }
  16.         public void Method2(string Msg)
  17.         {
  18.             Console.WriteLine(Msg);
  19.         }
  20.     }
  21.     public class B
  22.     {
  23.         public delegate void ShowMsg(string Msg);
  24.         public static void Main()
  25.         {
  26.             ShowMsg objShowMsg = BroadCastDelegate.Printf;
  27.             ShowMsg objShowMsg1 = (new BroadCastDelegate()).Method1;
  28.             ShowMsg objShowMsg2 = (new BroadCastDelegate()).Method2;
  29.             ShowMsg allShow = objShowMsg1 + objShowMsg2;
  30.             allShow += objShowMsg;
  31.             objShowMsg("Static Method");
  32.             objShowMsg1("Msg 1");
  33.             objShowMsg2("Msg 2");
  34.             allShow("Static Method");
  35.             Console.ReadLine();
  36.         }
  37.     }
  38.            
  39. }

注意他们的执行顺序。多播委托以先后为顺序

发表于 @ 2008年10月24日 19:16:00 | 评论( loading... ) | 编辑| 举报| 收藏

旧一篇:委托返回值 | 新一篇:C#中类的索引器

  • 发表评论
  • 评论内容:
  •  
Copyright © andyhooo
Powered by CSDN Blog