kingBook

导航

C# 委托

以下示例声明名为 Del 的委托,该委托可以封装采用字符串作为参数并返回 void 的方法:

public delegate void Del(string message);

委托对象通常通过提供委托将封装的方法的名称或使用匿名方法构造。对委托进行实例化后,委托会将对其进行的方法调用传递到该方法。调用方传递到委托的参数将传递到该方法,并且委托会将方法的返回值(如果有)返回到调用方。这被称为调用委托。实例化的委托可以按封装的方法本身进行调用。例如:

// Create a method for a delegate.
public static void DelegateMethod(string message)
{
    System.Console.WriteLine(message);
}


// Instantiate the delegate.
Del handler = DelegateMethod;

// Call the delegate.
handler("Hello World");

 

Action<T>委托

命名空间:   System

您可以使用 Action<T> 委托作为参数传递方法,而无需显式声明自定义的委托。 封装的方法必须对应于此委托定义的方法签名。 这意味着,封装的方法必须具有通过值传递给它的一个参数并且不能返回一个值。 (在 C# 中,该方法必须返回 void。)

using UnityEngine;
using System.Collections;
using System;

public class Test : MonoBehaviour {

    void Start () {
        a (b);
    }
  void a(Action<int,string>func){
        func (2, "d");
    }
    void b(int a,string b){
        Debug.Log (a+","+b);
    }


}

 Func<T1,...,TResult>

Func<TResult>委托

Func引用的方法必须有返回值,如果有参数则不能超过16个

using UnityEngine;
using System.Collections;
using System;

public class testFunc : MonoBehaviour {
    
    void Start () {
        runMake(make);//output: Lilei and Hanmeimei!
        runAbs(abs,-1);//ouput: 1
        runAdd(add,2.0f,3.0f);//ouput: 5
    }

    private void runMake(Func<string>func){
        Debug.Log (func());
    }
    private string make(){
        return "Lilei and Hanmeimei!";
    }

    private void runAbs(Func<float,float>func, float value){
        Debug.Log (func(value));
    }
    private float abs(float value){
        return value > 0 ? value : -value;
    }

    private void runAdd(Func<float,float,float>func, float a, float b){
        Debug.Log (func(a,b));
    }
    private float add(float aa,float bb){
        return aa + bb;
    }

}

 函数嵌套的实现

1.使用Action/Func.

using UnityEngine;
using System.Collections;
using System;

public class testEmbedFunc : MonoBehaviour {
    
    void Start () {
        //Func必须的返回值,可以没有参数,<>中的最后一项表示返回值类型
        Func<int,int, int> sum = new Func<int,int, int> ((int a,int b)=>{return a+b;});
        Debug.Log (sum(1,2));//output: 3

        Func<int,int, int> mul = new Func<int, int, int> ((int a,int b)=>{return a*b;});
        handlerA (mul);//output: 10

        //Action必须返回void,可以没有参数,<>中的每一项表示参数类型
        Action<int> logInt = new Action<int> ((int a)=>{Debug.Log (a);});
        logInt (12);//output: 12

        Action logHello = new Action (()=>{Debug.Log ("Hello");});
        logHello ();//output: Hello
        handlerB (logHello);//output: Hello
    }

    private void handlerA(Func<int,int,int> callback){
        Debug.Log (callback (5,2));
    }

    private void handlerB(Action callback){
        callback ();
    }
    
}

 2.使用匿名委托。

using UnityEngine;
using System.Collections;

public class testDelagate : MonoBehaviour {

    public delegate  float RunAbsCallback(float value);

    void Start () {
        RunAbsCallback abs = delegate (float value) {return value>0?value:-value;};//匿名委托不需要定义返回值类型
        Debug.Log (abs(-3.141f));//output: 3.141
        Debug.Log (runAbs (abs,-6.0f));//output: 6
    }

    private float runAbs(RunAbsCallback callback,float value){
        return callback (value);
    }

}

 

posted on 2016-04-12 17:10  kingBook  阅读(301)  评论(0编辑  收藏  举报