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

推荐订阅源

Attack and Defense Labs
Attack and Defense Labs
The GitHub Blog
The GitHub Blog
C
Check Point Blog
博客园_首页
MongoDB | Blog
MongoDB | Blog
N
Netflix TechBlog - Medium
F
Full Disclosure
Microsoft Security Blog
Microsoft Security Blog
爱范儿
爱范儿
Recent Announcements
Recent Announcements
阮一峰的网络日志
阮一峰的网络日志
G
GRAHAM CLULEY
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Threat Research - Cisco Blogs
C
Cybersecurity and Infrastructure Security Agency CISA
V
Vulnerabilities – Threatpost
K
Kaspersky official blog
博客园 - 司徒正美
S
Schneier on Security
T
The Exploit Database - CXSecurity.com
Project Zero
Project Zero
云风的 BLOG
云风的 BLOG
Cisco Talos Blog
Cisco Talos Blog
Know Your Adversary
Know Your Adversary
雷峰网
雷峰网
V
V2EX - 技术
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Spread Privacy
Spread Privacy
罗磊的独立博客
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
S
Security Affairs
SecWiki News
SecWiki News
Schneier on Security
Schneier on Security
O
OpenAI News
Jina AI
Jina AI
PCI Perspectives
PCI Perspectives
Cyberwarzone
Cyberwarzone
Y
Y Combinator Blog
Apple Machine Learning Research
Apple Machine Learning Research
B
Blog RSS Feed
I
InfoQ
D
Docker
P
Palo Alto Networks Blog
Recorded Future
Recorded Future
M
MIT News - Artificial intelligence
博客园 - Franky
B
Blog
Scott Helme
Scott Helme
博客园 - 叶小钗
D
DataBreaches.Net

博客园 - visi

【转】Linux常用命令大全 CIDR - visi Metro风格应用程序的定位功能 拒绝关机理由 Windows server2008 R2 添加快速启动工具栏 Metro风格应用程序的UI基本概念 MS outlook 邮件换行问题 aspnet下载文件到数据库并输出 母版页site.master sharepoint HTTP Error 503 - visi 【转】windows server 2008 域中更改密码策略 window上使用GIT的个人经验(入门级) 使用google jquery api VS里的配置数据库连接窗口调用 MY-SQL常用命令 SQL Server 的最大容量规范 修改WindowsServer2008系统文件权限 加入域错误,找不到网络路径 终于装上了office2010
Action委托
visi · 2011-09-01 · via 博客园 - visi

.net3.5,4.0中定义了Action关键字

 封装一个方法,该方法不具有参数并且不返回值。 

使用此委托以参数形式传递方法,不必显式定义一个封装无参数过程的委托。

若要引用无参数并返回值 的方法,请改用泛型 Func<TResult>委托。

using System; using System.Windows.Forms;
public delegate void ShowValue();
public class Name {
   
private string instanceName;    public Name(string name)    {       this.instanceName = name;    }
   
public void DisplayToWindow()    {       MessageBox.Show(this.instanceName);
   }
}
public class testTestDelegate {
   
public static void Main()    {       Name testName = new Name("Koani");       ShowValue showMethod = testName.DisplayToWindow;
      showMethod();
   }
}

 用Action委托:

public class testTestDelegate
{
   
public static void Main()
   {
      Name testName 
= new Name("Koani");
      Action showMethod 
= testName.DisplayToWindow;
      showMethod();
   }
}

用Action+匿名委托

public class Anonymous
{
   
public static void Main()
   {
      Name testName 
= new Name("Koani");
      Action showMethod 
= delegate() { testName.DisplayToWindow();} ;
      showMethod();
   }
}

用Action+Lambda 

public class LambdaExpression
{
   
public static void Main()
   {
      Name testName 
= new Name("Koani");
      Action showMethod 
= () => testName.DisplayToWindow();
      showMethod();
   }
}

那如果要传递的方法有参数怎么办,答案是用Action<T>

Action<T>有15个重载版本

同样Func<T>也有15个重载版本

View Code 

    public delegate void ShowValue();
    
public delegate void ShowValueName(string msg);
    
public class CustomObj
    {
        
public void Display()
        {
            Console.Write(
"hello!");
            Console.Read();
        }
public void DisplayName(string name)
        {
            Console.Write(
"hello," + name);
            Console.Read();
        }

View Code 

using System;
using System.Collections.Generic;
using System.Text;namespace ActionApp
{
    
class Program
    {
        
static void Main(string[] args)
        {
            CustomObj obj 
= new CustomObj();
            
//ShowValue sv = obj.Display;
            
//sv();

            Action a 
= obj.Display;
            a();
//Action a = delegate { obj.Display(); };
            
//a();//Action a = () => obj.Display();
            
//a();//ShowValueName svn = obj.DisplayName;
            
//svn("dd");//Action<string> at = obj.DisplayName;
            
//at("dd");//Action<string> at = delegate(string s) { obj.DisplayName(s);};
            
//at("dd");//Action<string> at = s => obj.DisplayName(s);
            
//at("dd");
        }
    }