Func<T>与Action<T>委托泛型介绍:转
.Net 3.5之后,微软推出了Func<T>与Action<T>泛型委托。进一步简化了委托的定义。
Action<T>委托主要的表现形式如下:
public delegate void Action();
public delegate void Action<T1>(T1 arg1);
public delegate void Action<T1, T2>(T1 arg1, T2 arg2);
public delegate void Action<T1, T2, T3>(T1 arg1, T2 arg2, T3 arg3);
public delegate void Action<T1, T2, T3, T4>(T1 arg1, T2 arg2, T3 arg3, T4 arg4);
public delegate void Action<T1, T2, T3, T4, T5>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5);
从Action<T>的定义形式上可以看到。Action<T>是没有返回值得。适用于任何没有返回值得方法。例如:

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
//同步执行
Action Action = new Action(writeLine);
Action.Invoke();
//异步执行
Action ActionAsy = new Action(writeLine2);
ActionAsy.BeginInvoke(resual=>Console.WriteLine("异步执行结束"), null);
Console.Read();
}
private static void writeLine()
{
Console.WriteLine("Action同步执行委托");
}
private static void writeLine2()
{
Console.WriteLine("Action异步执行委托");
}

如果调用Lambda表达式,可以更简练,对上面的代码,可以这样写:

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
//同步执行 用Lambda表达式代替writeLine
Action Action = new Action(()=>Console.WriteLine("Action同步执行委托"));
Action.Invoke();
//异步执行 用Lambda表达式代替writeLine2
Action ActionAsy = new Action(()=>Console.WriteLine("Action异步执行委托"));
ActionAsy.BeginInvoke(resual=>Console.WriteLine("异步执行结束"), null);
Console.Read();
}
private static void writeLine()
{
Console.WriteLine("Action同步执行委托");
}
private static void writeLine2()
{
Console.WriteLine("Action异步执行委托");
}

如果有参数需要传入,Action<T>可以这么做,例如:

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
//同步执行 传入一个参数
Action<string> Action = new Action<string>((a)=>Console.WriteLine(string.Format("Action同步执行委托,传入参数:{0}",a)));
Action.Invoke("小李");
//异步执行 传入两个参数
Action<string,int> ActionAsy = new Action<string,int>((a,b)=>Console.WriteLine("Action异步执行委托,传入参数:{0},{1}",a,b));
ActionAsy.BeginInvoke("小李",12,resual=>Console.WriteLine("异步执行结束"), null);
Console.Read();
}

在上面代码中,同步定义的string类型,必须保证传入的参数a也是string。虽然并没有对a进行类型定义,但是系统默认就是事先泛型中定义的类型。类似的,异步委托也是一样。不然会报错。
Func<T>委托主要的表现形式如下:
public delegate TResult Func<TResult>();
public delegate TResult Func<T1, TResult>(T1 arg1);
public delegate TResult Func<T1, T2, TResult>(T1 arg1, T2 arg2);
public delegate TResult Func<T1, T2, T3, TResult>(T1 arg1, T2 arg2, T3 arg3);
public delegate TResult Func<T1, T2, T3, T4, TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4);
public delegate TResult Func<T1, T2, T3, T4, T5, TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5);
Func<T>委托的定义是相对于Action<T>来说。Action<T>是没有返回值得方法委托,Func<T>是有返回值的委托。返回值的类型,由泛型中定义的类型进行约束。例如:

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
//异步执行
Func<string> FuncAsy = new Func<string>(() =>
{
people tPeo = new people("异步小李", 10);
return tPeo.ToString();
}
);
FuncAsy.BeginInvoke(resual =>
{
//异步执行,从回调函数中获取返回结果
Console.WriteLine(FuncAsy.EndInvoke(resual));
Console.WriteLine("异步执行结束");
}, null);
//同步执行
Func<string> Func = new Func<string>(() =>
{
people tPeo = new people("同步小李", 12);
return tPeo.ToString();
}
);
//同步执行,获取返回结果
Console.WriteLine(Func.Invoke());
Console.Read();
}
public class people
{
public string Name { get; set; }
public int Age { get; set; }
public people(string pName, int pAge)
{
this.Name = pName;
this.Age = pAge;
}
public override string ToString()
{
return string.Format("名称叫{0},年龄{1}", this.Name, this.Age);
}
}

输出结果如下:
如果有参数,可以这样写:

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
//异步执行 传入一个people类型的参数,返回一个sting类型的结果
Func<people, string> FuncAsy = new Func<people, string>((pPeople) =>
{
return pPeople.Name;
}
);
FuncAsy.BeginInvoke(new people("异步小李", 12), resual =>
{
//异步执行,从回调函数中获取返回结果
Console.WriteLine(FuncAsy.EndInvoke(resual));
Console.WriteLine("异步执行结束");
}, null);
//同步执行 传入一个string,int类型的参数,返回一个people类型的结果
Func<string, int, people> Func = new Func<string, int, people>((pName,pAge) =>
{
people tPeo = new people(pName, pAge);
return tPeo;
}
);
//同步执行,返回结果
Console.WriteLine(Func.Invoke("同步小李",12).ToString());
Console.Read();
}

作者: cglnet
Func<T>与Action<T>委托泛型介绍:转的更多相关文章
- Func<T>与Action<T>委托泛型介绍
.Net 3.5之后,微软推出了Func<T>与Action<T>泛型委托.进一步简化了委托的定义. Action<T>委托主要的表现形式如下: public de ...
- 委托, 泛型委托,Func<T>和Action<T>
使用委托来做一些事情,大致思路是: 1.定义声明一个委托,规定输入参数和输出类型.2.写几个符合委托定义的方法.3.把方法列表赋值给委托4.执行委托 internal delegate int MyD ...
- C#委托的介绍(delegate、Action、Func、predicate)
委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递.事件是一种特殊的委托. 1.委托的声明 (1). delegate delegate我们常用到的一种声明 Deleg ...
- C#委托的介绍(delegate、Action、Func、predicate) --转载
来源:http://www.cnblogs.com/akwwl/p/3232679.html 委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递.事件是一种特殊的委托. 1 ...
- 【转】C# 委托的介绍(delegate、Action、Func、predicate)
委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递.事件是一种特殊的委托. 1.委托的声明 (1). delegate delegate我们常用到的一种声明 Delegat ...
- C#委托的介绍(delegate、Action、Func、predicate)(转)
委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递.事件是一种特殊的委托. 1.委托的声明 (1). delegate delegate我们常用到的一种声明 Deleg ...
- C#委托的介绍(delegate、Action、Func、predicate)【转】
转自 http://www.cnblogs.com/akwwl/p/3232679.html 委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递.事件是一种特殊的委托. 1 ...
- C#委托的介绍(delegate、Action、Func、predicate)ga
转载:http://www.cnblogs.com/akwwl/p/3232679.html 感觉写的很好.例子也很简单明了.赞一个 委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参 ...
- System.Func<>与System.Action<>
使用并行编程可以同时操作多个委托,在介绍并行编程前先简单介绍一下两个泛型委托System.Func<>与System.Action<>. Func<>是一个能接受多 ...
随机推荐
- oracle检查点队列(checkpoint queue)
buffer cache CBC链 按地址链 LRU 干净buffer LRUW 脏buffer 按照冷热 checkpoint queue:链buffer,①链脏块②按buffer第一次脏的时 ...
- JDK 1.5 1.6 override区别
今天在更新时发现有个别项目报错,报错信息 到网上搜索了之后,根据网上描述,修改了一批配置都不行: http://bestchenwu.iteye.com/blog/997420(这个里面的方法二,即为 ...
- 信号量进程同步,王明学learn
信号量进程同步 一组并发进程进行互相合作.互相等待,使得各进程按一定的顺序执行的过程称为进程间的同步. 信号量在进程同步时初始值为:0 信号量在进程互斥时初始值为:大于0的 本章节主要使用信号量,使的 ...
- Linux环境变量设置指南
以配置java环境变量为例 目录 [隐藏] 1 修改/etc/profile文件 2 修改用户目录下的.bash_profile 3 修改.bashrc文件 4 直接在shell下设置 5 查看环境 ...
- C语言补码作用
补码主要是为了cpu运算器在进行减法运算时避免借位而设立的. 在早期,cpu中的运算器部分,只要实现一个加法器就可以完成四由算术运算. 因为计算机中的数值编码是有限位数的,所以减法实际上相当于加上减数 ...
- class.forname()用法 转
主要功能 Class.forName(xxx.xx.xx)返回的是一个类 Class.forName(xxx.xx.xx)的作用是要求JVM查找并加载指定的类, 也就是说JVM会执行该类的静态代码段 ...
- 《DSP using MATLAB》示例Example4.10
上代码: b = [1, 0.4*sqrt(2)]; a = [1, -0.8*sqrt(2), 0.64]; % compute the polynomials coefficients given ...
- shell-bash学习04读取输入、分隔符、流程控制
读入输出 输入通常是通过stdin或参数传递给命令; 输出出现在stderr或stdout; 管道,过滤器,管道操作符: cmd1 | cmd2 | cmd3; //最后还有输出 ls | cat - ...
- mybaties 的一些点
resultMap resutType mybaties缓存 待续 mybaties对应关系是bean和数据库字段的对应. 1.mybaties 的返回值是对象的话定义为resultMap=" ...
- iOS __strong __weak @Strongify @Weakify
@Strongify,@Weakify主要是在block中使用. 因为block一般都在对象内部声明.. 如果在block内部使用了当前对象的属性,就会造成循环引用(block拥有当前对象的地址,而当 ...