一、说明

一般我们定义委托都是有如下两步:

public delegate void MyDelegate(string name);//定义委托
public MyDelegate myDelegate; //使用委托

但.Net也提供了定义好的委托,我们可以直接使用。

二、定义

System.Action 无返回值

Action:
public delegate void Action (); Action< T >:
public delegate void Action< T > (T obj); Action< T1, T2 >:
public delegate void Action< T1, T2 > (T1 arg1, T2 arg2);
* delegate void Action<T1,T2,T3,T4>T1 arg1, T2 arg2, T3 arg3, T4 arg4);

System.Func 有返回值

Func< TResult >
public delegate TResult Func< TResult > (); Func< T,TResult >
public delegate TResult Func< T, TResult > (T arg); Func< T1,T2,TResult >
public delegate TResult Func< T1, T2, TResult > (T1 arg1, T2 arg2);
*delegate TResult Func<T1,T2,T3,T4,TResult>T1 arg1, T2 arg2, T3 arg3, T4 arg4);

三、示例理解

例子1:Action

using UnityEngine;
using System.Collections;
using System;
public class ActionTest : MonoBehaviour {
void Start () {
Action action = XXX;
action();
}
void XXX()
{
Debug.Log("100");
}
}

例子2:Action<T>

using UnityEngine;
using System.Collections;
using System;
public class ActionTest : MonoBehaviour {
void Start () {
Action<string> action = XXX;
action("unity C#");
}
void XXX(string name)
{
Debug.Log(name);
}
}

例子3:Action<T1,T2>

using UnityEngine;
using System.Collections;
using System;
public class ActionTest : MonoBehaviour {
void Start () {
Action<string,int> action = XXX;
action("unity C#",100);
}
void XXX(string name,int score)
{
Debug.Log(string.Format("{0} {1}",name,score);
}
}
 
#region Action的用法
///Action<T>的用法
///这里的T为代理函数的传入类型,无返回值
Action<string[]> action = delegate(string[] x)
{
var result = from p in x
where p.Contains("s")
select p;
foreach (string s in result.ToList())
{
Console.WriteLine(s);
}
};
string[] str={ "charlies","nancy","alex","jimmy","selina"};
action(str);
Console.ReadKey();
#endregion

上面的例子是通过传入的String类型的数组,找出其中包含有字符s的项,然后输出到控制台。

例子4:Func<TResult >

using UnityEngine;
using System.Collections;
using System;
public class FuncTest : MonoBehaviour {
void Start () {
Func< int > func= XXX;
Debug.Log( func() );
}
int XXX()
{
return 10;
}
}

例子5: Func<T,TResult>

using UnityEngine;
using System; public Class FuncTest:MonoBehaviour{
void Start(){
Func<string ,int> func= CallStringLength;
} int CallStringLength(string str){
return str.Lenth;
} }
Func<string> func=delegate(){
return "我是Func<TResult>委托返回的结果";
}
 

Predicate只能接受一个传入参数,返回值为bool类型
#region Predicate
///bool Predicate<T>的用法
///输入一个T类型的参数,返回值为bool类型
Predicate<string[]> predicate = delegate(string[] x)
{
var result = from p in x
where p.Contains("s")
select p;
if (result.ToList().Count > 0)
{
return true;
}
else
{
return false;
}
};
string[] _value = { "charlies", "nancy", "alex", "jimmy", "selina" };
if (predicate(_value))
{
Console.WriteLine("They contain.");
}
else
{
Console.WriteLine("They don't contain.");
}
Console.ReadKey();
#endregion

上面的代码其实也是判断String数组中有没有包含s的项,有的话就在控制台打印出  They contain.没有的话就打印出They don't contain
 
//定义
public void CallUI<T>(Action<T, object[]> callback, params object[] args) where T : CUIBase
//调用
CUIManager.Instance.CallUI<CUIMidMsg>(
(_ui, _arg) => _ui.ShowMsg((string)_arg[0]),
string.Format(szMsg, format));
 

资料

部分内容参考自:风宇冲Unity3D教程学院

[C#] 委托之Action和Func区别的更多相关文章

  1. C#系统委托之Action And Func

    Action Action<T> Func Func<T> Action:封装一个方法,该方法不具有参数并且不返回值 public delegate void Action() ...

  2. Action<>和Func<>区别

    Action<>和Func<>其实都是委托的[代理]简写形式. 简单的委托写法: //普通的委托 public delegate void myDelegate(string ...

  3. 浅析C#之委托、Action、Func

    一.委托 1.1 委托的定义 delegate(委托)是一种可用于封装命名方法或匿名方法的引用类型, 委托类似于 C++ 中的函数指针: .Net通过委托来提供回调函数机制. 声明一个委托类型 int ...

  4. Action<>和Func<> 区别

    其实他们两个都是委托[代理]的简写形式. 一.[action<>]指定那些只有输入参数,没有返回值的委托 Delegate的代码: public delegate void myDeleg ...

  5. 委托、Action、Func使用

    参考 using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syste ...

  6. 事件,委托,action与func文章不错的

    https://www.cnblogs.com/yinqixin/p/5056307.html https://www.cnblogs.com/BLoodMaster/archive/2010/07/ ...

  7. C#中匿名函数、委托delegate和Action、Func、Expression、还有Lambda的关系和区别

    以前一直迷迷糊糊的,现在总算搞明白. Lambda表达式 Lamda表达式基本写法是()=>{ };Lambda和方法一样都可以传入参数和拥有返回值.(int x)=>{return x; ...

  8. Func 委托 和 Action 委托 初步谈论

    继上篇EventHandler之后,继续填坑,简单了解下Func<TResult> 委托 和 Action 委托. msdn对于两者的解释: Func<TResult>:封装一 ...

  9. C#常见委托のdelegate定义,Func,Action,Predicate总结

    委托,顾名思义,就是让其他代理,本质就是为具有共性方法组定义一个方法模板:(交流可以加qq群:435226676) 委托常见的方式有一般委托显示定义,Func<T,TResult> (T, ...

随机推荐

  1. PlayFramework 1 自定义标签 -- FastTags http://unmi.cc/category/javajee/playframework/

    最早是用 HTML 来自定义标签,现在觉得 HTML 写有关逻辑的代码就有点不伦不类了,HTML 里着重是显示代码.前有一篇  PlayFramework 1 模板应用 -- Java 对象扩展 学习 ...

  2. 养只爬虫当宠物(Node.js爬虫爬取58同城租房信息)

    先上一个源代码吧. https://github.com/answershuto/Rental 欢迎指导交流. 效果图 搭建Node.js环境及启动服务 安装node以及npm,用express模块启 ...

  3. CSS之浮动那些事

    1.清除浮动 下面是两种常用的方式,而这两招也够用了(不用千招会,只需一招精). 1.结尾处加空div标签 clear:both <style type="text/css" ...

  4. 存放事务码及其描述的TABLE是TSTC、TSTCT。

    存放事务码及其描述的TABLE是TSTC.TSTCT.

  5. .NET破解之太乐地图下载器【非暴破】

    不知不觉,接触破解逆向已经三个月了,从当初的门外汉到现在的小白,这个过程只有经历过才知道其中的苦与乐: 有无知.困惑.痛苦.惊喜.彻悟.欣慰…… 有无助的软件脱壳,茫然的代码分析,有无趣的反复测试, ...

  6. 转:HTTP 1.1与HTTP 1.0的比较

    原文地址:http://blog.csdn.net/elifefly/article/details/3964766 HTTP 1.1与HTTP 1.0的比较 一个WEB站点每天可能要接收到上百万的用 ...

  7. Sharepoint学习笔记—习题系列--70-573习题解析 -(Q107-Q110)

    Question 107You are creating a custom workflow action that will be used in Microsoft SharePoint Desi ...

  8. 【原】就IOS发布app时如何保护文本资源的一个方法

    近期的一个app是本地的,数据源来自于本地的一个.json文件,里面的数据是这个app的灵魂.近期快发布该app了,很担心发布后的.ipa包被竞争者解开然后信息发生泄漏.我的处理策略是:打包的时候放的 ...

  9. 全球最低功耗蓝牙单芯片DA14580的硬件架构和低功耗

    号称全球最低功耗蓝牙单芯片DA14580在可穿戴市场.健康医疗.ibeacon定位等市场得到广泛的应用,但是因为其较为封闭的技术/资料支持导致开发人员有较高的技术门槛,网络上也极少看到有关DA1458 ...

  10. 做一些Spring AOP做过的事,封装 jdk动态代理成为一个黑盒子

      怎么使用eclise 抽取方法,请看  利用eclipse 抽取代码片段为方法   抽取完成之后,还需要 ① 将Collection.class换成  target.getClass(),targ ...