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

推荐订阅源

Hacker News: Ask HN
Hacker News: Ask HN
WordPress大学
WordPress大学
T
The Blog of Author Tim Ferriss
The GitHub Blog
The GitHub Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东
A
About on SuperTechFans
Stack Overflow Blog
Stack Overflow Blog
雷峰网
雷峰网
Microsoft Azure Blog
Microsoft Azure Blog
腾讯CDC
爱范儿
爱范儿
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 【当耐特】
V
Visual Studio Blog
有赞技术团队
有赞技术团队
U
Unit 42
D
Docker
小众软件
小众软件
F
Full Disclosure
I
Intezer
Scott Helme
Scott Helme
P
Privacy International News Feed
P
Proofpoint News Feed
Engineering at Meta
Engineering at Meta
Google DeepMind News
Google DeepMind News
B
Blog
Martin Fowler
Martin Fowler
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Vercel News
Vercel News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Spread Privacy
Spread Privacy
宝玉的分享
宝玉的分享
S
Security Affairs
www.infosecurity-magazine.com
www.infosecurity-magazine.com
月光博客
月光博客
C
Cisco Blogs
云风的 BLOG
云风的 BLOG
Schneier on Security
Schneier on Security
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Threat Research - Cisco Blogs
量子位
Hacker News - Newest:
Hacker News - Newest: "LLM"
H
Heimdal Security Blog
N
Netflix TechBlog - Medium
H
Hacker News: Front Page
P
Proofpoint News Feed
G
GRAHAM CLULEY
V
Vulnerabilities – Threatpost
S
Schneier on Security

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