惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
C
Check Point Blog
J
Java Code Geeks
腾讯CDC
Martin Fowler
Martin Fowler
MongoDB | Blog
MongoDB | Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园 - 三生石上(FineUI控件)
Apple Machine Learning Research
Apple Machine Learning Research
大猫的无限游戏
大猫的无限游戏
Engineering at Meta
Engineering at Meta
罗磊的独立博客
Last Week in AI
Last Week in AI
B
Blog
IT之家
IT之家
S
SegmentFault 最新的问题
D
DataBreaches.Net
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
GbyAI
GbyAI
博客园 - 聂微东
U
Unit 42
有赞技术团队
有赞技术团队
Y
Y Combinator Blog
MyScale Blog
MyScale Blog

博客园 - Caviare

前台优化法则(YSlow) ASP.NET MVC源代码 启用IIS的Gzip压缩功能 web.sitemap Dynamic Generation SQL字符正则匹配 WorkBook操作实例 判断大陆IPAddress 迭代器和排序基本使用 Bertrand Meyer提出的设计模式的原则 - Caviare - 博客园 Web.cofig详解[转] UpdatePanel Control [转老赵博客] .Net环境下的缓存技术介绍 (转) [转]webservice和remoting在分布式程序中的应用 sql2005备份在sql2000中恢复 Excel WorkBook操作例 温故知新-Excel操作类 SQL collation设置 一张类的访问权限图,帮您加深概念 [转]您可能不知道的Asp.Net2.0技巧
委托应用
Caviare · 2008-01-08 · via 博客园 - Caviare

简单整理了下委托的最常见应用,代码比较乱

  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