简单整理了下委托的最常见应用,代码比较乱
  1using System;
  2/// <summary>
  3/// 简单的委托
  4/// </summary>

  5public class TestDelegate1
  6{
  7    public delegate void ProcessDelegate(string str);
  8    string strTemp;
  9    void Process1(string str)
 10    {
 11        Console.WriteLine("func1" + str);
 12    }

 13    void Process2(string str)
 14    {
 15        Console.WriteLine("func2" + str);
 16    }

 17    ProcessDelegate d;
 18    void Main()
 19    {
 20        d = new ProcessDelegate(Process1);
 21        d("aaa");
 22        d = new ProcessDelegate(Process2);
 23        d("bbb");
 24    }

 25}

 26/// <summary>
 27/// 泛型委托
 28/// </summary>

 29public class TestDelegate2
 30{
 31    public delegate string ProcessDelegate<T, S>(T t, S s);
 32    string Process1(string s, int i)
 33    {
 34        return s + i;
 35    }

 36    string Process2(int s, int i)
 37    {
 38        return s.ToString() + i.ToString();
 39    }

 40    void Main()
 41    {
 42        ProcessDelegate<stringint> d1;
 43        ProcessDelegate<intint> d2;
 44        d1 = new ProcessDelegate<stringint>(Process1);
 45        d2 = new ProcessDelegate<intint>(Process2);
 46        Console.WriteLine(d1("s"100));
 47        Console.WriteLine(d2(100100));
 48    }

 49}

 50/// <summary>
 51/// 回调函数
 52/// </summary>

 53public class TestDelegate3
 54{
 55    void Main()
 56    {
 57        fun("aaa""bbb", Process1);
 58        fun("aaa""bbb", Process2);
 59    }

 60    public delegate string ProcessDelegate(string str1, string str2);
 61    string Process1(string str1, string str2)
 62    {
 63        return "Process1" + str1 + str2;
 64    }

 65    string Process2(string str1, string str2)
 66    {
 67        return "Process2" + str1 + str2;
 68    }

 69    void fun(string str1, string str2, ProcessDelegate Process)
 70    {
 71        Console.WriteLine(Process(str1, str2));
 72    }

 73}

 74/// <summary>
 75/// 事件委托
 76/// </summary>

 77public class TestDelegate4
 78{
 79    void Main()
 80    {
 81        ProcessEvent += new ProcessDelegate(OnProcessAction);
 82    }

 83    void OnProcessAction(object sender, EventArgs e)
 84    {
 85        //do sth on action
 86    }

 87
 88    public delegate void ProcessDelegate(object sender, EventArgs e);
 89    public event ProcessDelegate ProcessEvent;
 90    public void ProcessAction(object sender, EventArgs e)
 91    {
 92        if (ProcessEvent == null)
 93        {
 94            ProcessEvent(sender, e);
 95        }

 96    }

 97}

 98/// <summary>
 99/// 匿名方法
100/// </summary>

101public class TestDelegate5 : System.Web.UI.Page 
102{
103    System.Web.UI.WebControls.Button btn = new System.Web.UI.WebControls.Button();
104    void Main()
105    {
106        //无参数
107        this.Load += delegate { Response.Write("this is anonymous method"); };
108        //带参数
109        btn.Click += delegate(object sender, EventArgs e) {
110            ((System.Web.UI.WebControls.Button)sender).Text = "Is Clicked";
111        }
;
112    }

113}

114

posted on 2008-01-08 16:42  Caviare  阅读(424)  评论(0编辑  收藏  举报