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   kingBook  阅读(307)  评论(0编辑  收藏  举报

编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
历史上的今天:
2015-04-12 haxe 中使用音效
2015-04-12 haxe 嵌入swf 读取里面的内容
2015-04-12 haxe 配置
点击右上角即可分享
微信分享提示