[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教程学院


本文转自赵青青博客园博客,原文链接:http://www.cnblogs.com/zhaoqingqing/p/3778716.html,如需转载请自行联系原作者

上一篇:[20170628]11g修改用户名.txt


下一篇:通过IL分析C#中的委托、事件、Func、Action、Predicate之间的区别与联系