.net中的各种委托(Delegate、Action、Func)
1、Delegate,委托的鼻祖
protected delegate int ClassDelegate(int x, int y);//定义委托类型及参数
static void Main(string[] args)
{
ClassDelegate dele = new ClassDelegate(Add);//实例化一个委托 Console.WriteLine(dele(, ));//调用委托
Console.ReadKey();
} static int Add(int a, int b)
{
return a + b;
}
2、Action,可以传入参数,没有返回值的委托
方法1,调用方法
static void Main(string[] args)
{
Action<int, int> ac = new Action<int, int>(ShowAddResult);//实例化一个委托
ac(, );//调用委托 Console.ReadKey();
} static void ShowAddResult(int a, int b)
{
Console.WriteLine(a + b);
}
方法2,使用lambda表达式
static void Main(string[] args)
{
Action<int, int> ac = ((p, q) => Console.WriteLine(p + q));//实例化一个委托
ac(, );//调用委托 Console.ReadKey();
}
方法3,作为参数传
static void Main(string[] args)
{
Action<string> ac = (p => Console.WriteLine("我是方法1,传入值:"+p));//实例化一个委托
Action<string> ac2 = (p => Console.WriteLine("我是方法2,传入值:" + p));//实例化另一个委托 Test(ac, "参数1");//调用test方法,传入委托参数
Test(ac2, "参数1");//调用test方法,传入委托参数 Console.ReadKey();
} static void Test<T>(Action<T> ac, T inputParam)
{
ac(inputParam);
}
3、Func,可以传入参数,必须有返回值的委托
方法1,调用方法
static void Main(string[] args)
{
Func<string> fc1 = new Func<string>(ShowAddResult);//实例化一个委托
string result = fc1();//调用委托 Console.WriteLine(result);
Console.ReadKey();
}
static string ShowAddResult()
{
return "地球是圆的";
}
方法2,使用lambda表达式
static void Main(string[] args)
{
//实例化一个委托,注意不加大括号,写的值就是返回值,不能带return
Func<string> fc1 = () => "地球是圆的"; //实例化另一个委托,注意加大括号后可以写多行代码,但是必须带return
Func<string> fc2 = () =>
{
return "地球是圆的";
}; string result = fc1();//调用委托
string result2 = fc2();//调用委托 Console.WriteLine(result);
Console.WriteLine(result2);
Console.ReadKey();
}
方法3,作为参数传
static void Main(string[] args)
{
//实例化一个委托,注意不加大括号,写的值就是返回值,不能带return
Func<int, string> fc1 = (p) => "传入参数" + p + ",地球是圆的"; //实例化另一个委托,注意加大括号后可以写多行代码,但是必须带return
Func<string, string> fc2 = (p) =>
{
return "传入参数" + p + ",地球是圆的";
}; string result = Test<int>(fc1, );//调用委托
string result2 = Test<string>(fc2, "");//调用委托 Console.WriteLine(result);
Console.WriteLine(result2);
Console.ReadKey();
} static string Test<T>(Func<T, string> fc, T inputParam)
{
return fc(inputParam);
}
总结:
Delegate至少0个参数,至多32个参数,可以无返回值,也可以指定返回值类型
Func可以接受0个至16个传入参数,必须具有返回值
Action可以接受0个至16个传入参数,无返回值
.net中的各种委托(Delegate、Action、Func)的更多相关文章
- 委托delegate,Action,Func,Predicate
C#委托的介绍(delegate.Action.Func.predicate) 委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递.事件是一种特殊的委托. 1.委托的声明 ...
- C# 委托应用总结(委托,Delegate,Action,Func,predicate)
C# 委托应用总结 一.什么是委托 1.1官方解释 委托是一种定义方法签名的类型.当实例化委托时,您可以将其实例与任何具有兼容签名的方法相关联.您可以通过委托实例调用方法. 1.2个人理解 委托就是执 ...
- Delegate,Action,Func,匿名方法,匿名委托,事件 (转载)
Delegate,Action,Func,匿名方法,匿名委托,事件 (转载) 一.委托Delegate 一般的方法(Method)中,我们的参数总是string,int,DateTime...这些基本 ...
- Delegate,Action,Func,匿名方法,匿名委托,事件
一.委托Delegate 一般的方法(Method)中,我们的参数总是string,int,DateTime...这些基本的数据类型(或者没有参数),比如 public void HelloWorld ...
- Delegate,Action,Func,Predicate的使用与区别
C#4.0推出后,类似Linq,Lamda表达式等许多新的程序写法层次不穷.与之相关的Delegate,Action,Func,Predicate的使用和区别也常常让大家迷惑,此处就结合实际的应用,对 ...
- 【Unity|C#】基础篇(11)——内置的泛型委托(Action/Func/Predicate)
[Action] 无返回值 的泛型委托,可以有0~16个参数(函数重载) public delegate void Action(); // 无参数 public delegate void Acti ...
- 温故而知新:Delegate,Action,Func,匿名方法,匿名委托,事件
Tks: http://www.cnblogs.com/yjmyzz/archive/2009/11/23/1608818.html 20150801 add: http://www.cnblogs. ...
- 对委托 以及 action func 匿名函数 以及 lambda表达式的简单记录
class Program { public delegate void MyDelegate(string str); static void Main(string[] args) { // My ...
- lambda表达式不使用委托(delegate) 用FUNC
lLambda不使用delegate关键字,而使用 Lambda运算符 => goes to l 1.Func<int,string> getInput = (int age ...
随机推荐
- 【C++知识点】单例模式的简单实现
单例模式是最常见,也是使用最广泛的一种设计模式,其意图是保证一个类仅有一个实例,并提供一个访问它的全局访问点,该实例被所有程序模块共享. 单例模式的实现方法有很多种,本文只给出一个最简单的实现,如下: ...
- 【一天一道LeetCode】#89. Gray Code
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 The gra ...
- React Native运行原理解析
Facebook 于2015年9月15日推出react native for Android 版本, 加上2014年底已经开源的IOS版本,至此RN (react-native)真正成为跨平台的客户端 ...
- 【uWSGI】 实战之操作经验
以下是uWSGI版本为2.0以上,uwsgi的启动可以把参数加载命令行中,也可以是配置文件 .ini, .xml, .yaml 配置文件中,个人用的比较多得是 .ini 文件.下面总结下自己操作和使用 ...
- 【一天一道LeetCode】#73. Set Matrix Zeroes
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- Leetcode_169_Majority Element
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/42247887 Given an array of size ...
- ARM Linux内核Input输入子系统浅解
--以触摸屏驱动为例 第一章.了解linux input子系统 Linux输入设备总类繁杂,常见的包括有按键.键盘.触摸屏.鼠标.摇杆等等,他们本身就是字符设备,而linux内核将这些 ...
- HBASE表设计
1. 表的设计 1.1 Pre-Creating Regions 默认情况下,在创建HBase表的时候会自动创建一个region分区,当导入数据的时候,所有的HBase客户端都向这一个region写数 ...
- Xshell Linux 主要命令
1. 查看当前路径 pwd 2.列出当前目录的文件 ls 列出所有文件或者文件夹 ls *abc 列出以abc开头的所以文件 ls –l 列出所以文件及其详细详细 3.进入目录 cd ...
- Linux的常用命令(2) - 关机
关机命令 shutdown‑h now 立即进行关机 shutdown‑r now 现在重新启动计算机 -t sec : -t后面加秒数,即"过几秒后关机" -k : 不 ...