C# delegate 移除 方法

不多讲 直接上代码!  delegate 的原始 写法 :  (+=, -= )



using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public class DelegateScript : MonoBehaviour {

    delegate void MyDelegate(int num);
    void Start()
    {
        //创建3 个MyDelegate 委托类的实例
        MyDelegate myDelegatel = new MyDelegate(this.PrintNum);
        MyDelegate myDelegate2 = new MyDelegate(this.PrintDoubleNum);
        MyDelegate myDelegate3 = new MyDelegate(this.PrintTripleNum);


        MyDelegate myDelegates = null;
        //使用Delegate 类的静态方法Combine
        myDelegates = (MyDelegate)Delegate.Combine(myDelegates, myDelegatel);
        myDelegates = (MyDelegate)Delegate.Combine(myDelegates, myDelegate2);
        myDelegates = (MyDelegate)Delegate.Combine(myDelegates, myDelegate3);
        //将myDelegates 传入Print 方法
        this.Print(10, myDelegates);



        ///移除方法 
        myDelegates = (MyDelegate)Delegate.Remove(myDelegates, myDelegatel);
        ///移除所有
        myDelegates = (MyDelegate)Delegate.RemoveAll(myDelegates, myDelegatel);

    }

    private void Print(int v, MyDelegate myDelegates)
    {
        
    }

    private void PrintTripleNum(int num)
    {
        
    }

    private void PrintDoubleNum(int num)
    {
        
    }

    private void PrintNum(int num)
    {
         
    }
}


你可能感兴趣的:(C#)