大学IT网 - 最懂大学生的IT学习网站! QQ资料交流群:367606806
当前位置:大学IT网 > C#技巧 > C#委托Action和Func

C#委托Action和Func

关键词:委托ActionFuncC#  阅读(763) 赞(12)

[摘要]本文是对C#委托Action和Func的讲解,对学习C#编程技术有所帮助,与大家分享。

一、说明

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

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));


相关评论