[C#] 委托之Action和Func区别
一、说明
一般我们定义委托都是有如下两步:
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区别的更多相关文章
- C#系统委托之Action And Func
Action Action<T> Func Func<T> Action:封装一个方法,该方法不具有参数并且不返回值 public delegate void Action() ...
- Action<>和Func<>区别
Action<>和Func<>其实都是委托的[代理]简写形式. 简单的委托写法: //普通的委托 public delegate void myDelegate(string ...
- 浅析C#之委托、Action、Func
一.委托 1.1 委托的定义 delegate(委托)是一种可用于封装命名方法或匿名方法的引用类型, 委托类似于 C++ 中的函数指针: .Net通过委托来提供回调函数机制. 声明一个委托类型 int ...
- Action<>和Func<> 区别
其实他们两个都是委托[代理]的简写形式. 一.[action<>]指定那些只有输入参数,没有返回值的委托 Delegate的代码: public delegate void myDeleg ...
- 委托、Action、Func使用
参考 using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syste ...
- 事件,委托,action与func文章不错的
https://www.cnblogs.com/yinqixin/p/5056307.html https://www.cnblogs.com/BLoodMaster/archive/2010/07/ ...
- C#中匿名函数、委托delegate和Action、Func、Expression、还有Lambda的关系和区别
以前一直迷迷糊糊的,现在总算搞明白. Lambda表达式 Lamda表达式基本写法是()=>{ };Lambda和方法一样都可以传入参数和拥有返回值.(int x)=>{return x; ...
- Func 委托 和 Action 委托 初步谈论
继上篇EventHandler之后,继续填坑,简单了解下Func<TResult> 委托 和 Action 委托. msdn对于两者的解释: Func<TResult>:封装一 ...
- C#常见委托のdelegate定义,Func,Action,Predicate总结
委托,顾名思义,就是让其他代理,本质就是为具有共性方法组定义一个方法模板:(交流可以加qq群:435226676) 委托常见的方式有一般委托显示定义,Func<T,TResult> (T, ...
随机推荐
- mvc与三层结构终极区别
http://blog.csdn.net/csh624366188/article/details/7183872 http://www.cnblogs.com/zhhh/archive/2011/0 ...
- 捋一捋Javascript数据类型转换规则
一.数据类型 5种基本数据类型:Null/Undefined/String/Boolean/Number 1种复杂数据类型:Object 二.数据类型检测 传送门<几种JS数据类型方式及其局限性 ...
- javascript的封装实例
StringBuffer方法的js自定义封装: <!doctype html><html lang="en"> <head> <meta ...
- AE用线来分割线面(C#2010+AE10.0… .
希望指正. 在 ITools 类中,部分方法如下: public override void OnMouseDown(int Button, int Shift, int X, int Y) { if ...
- 桥牌笔记索引,牌例全部摘自Bridge Master 2000
Level 3 A A34-到处都是希望 B B14 防将牌失控 B26 探索式打法 C C1 绝不能让东家上手 C5 4-1分布该怎么办? C10 保持将牌控制 C27 多一个成功机会 D D1 注 ...
- 简析android消息模型
android总结系列 一.消息系统构成要素和基本原理 l 消息队列 l 发送消息 l 消息读取 l 消息分发 l 消息循环线程 消息系统必须要依赖一个消息循环线程来轮询自己的消息队列,如果 ...
- IOS Quartz2D简介
Quartz2D 简介( 后续会有相关应用) 第一部分 绘制直线 代码示例: - (void)drawRect:(CGRect)rect{ //获取图形上下文 CGContextRef cxConte ...
- 【转】Windows的多线程编程,C/C++
在Windows的多线程编程中,创建线程的函数主要有CreateThread和_beginthread(及_beginthreadex). CreateThread 和 ExitThread 使 ...
- Objective-C之代理设计模式小实例
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- ffmpeg2.2在ubuntu下使用NDK编译——并在android工程下测试使用
作者:wainiwann 出处:http://www.cnblogs.com/wainiwann/ 本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则 ...