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

推荐订阅源

Google DeepMind News
Google DeepMind News
S
Secure Thoughts
Application and Cybersecurity Blog
Application and Cybersecurity Blog
AWS News Blog
AWS News Blog
GbyAI
GbyAI
腾讯CDC
WordPress大学
WordPress大学
V
V2EX
小众软件
小众软件
C
CXSECURITY Database RSS Feed - CXSecurity.com
M
MIT News - Artificial intelligence
T
Troy Hunt's Blog
H
Hacker News: Front Page
Scott Helme
Scott Helme
The Hacker News
The Hacker News
Schneier on Security
Schneier on Security
K
Kaspersky official blog
S
Security @ Cisco Blogs
N
News | PayPal Newsroom
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
Threatpost
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Martin Fowler
Martin Fowler
P
Privacy International News Feed
Blog — PlanetScale
Blog — PlanetScale
D
DataBreaches.Net
O
OpenAI News
P
Proofpoint News Feed
J
Java Code Geeks
B
Blog RSS Feed
Attack and Defense Labs
Attack and Defense Labs
L
Lohrmann on Cybersecurity
T
Threat Research - Cisco Blogs
C
Cybersecurity and Infrastructure Security Agency CISA
阮一峰的网络日志
阮一峰的网络日志
P
Palo Alto Networks Blog
Engineering at Meta
Engineering at Meta
AI
AI
Google DeepMind News
Google DeepMind News
雷峰网
雷峰网
Microsoft Azure Blog
Microsoft Azure Blog
Simon Willison's Weblog
Simon Willison's Weblog
人人都是产品经理
人人都是产品经理
T
Tailwind CSS Blog
G
GRAHAM CLULEY
A
Arctic Wolf
N
Netflix TechBlog - Medium
L
LINUX DO - 最新话题
爱范儿
爱范儿
博客园 - 聂微东

博客园 - 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