C# 委托

委托(delegate):可以理解为函数的一个包装,使C#当中的函数可以作为参数进行传递

  • 委托是方法的抽象:它是存储一系列具有相同签名和返回类型的方法的地址,调用委托的时候,委托包装的方法将会被调用
  •  public delegate void MyDelegate(int a, string b);
  •  public void MyMethod(int aa,string bb){...}
  • 委托链的添加/移除
namespace Demo8_4
{
    
    class Program
    {
        public delegate string SayDel(string name);
        public static string SayA(string name)
        {
            return name + "————哈哈哈";
        }
        public static string SayB(string name)
        {
            return name + "————hello";
        }
        public static void SayAB(SayDel say, string name)
        {
            Console.WriteLine(say.Invoke(name));//Invoke调用方法的意思
        }
        static void Main(string[] args)
        {
            SayAB(SayA,"你好");
            SayAB(SayB,"hai");

            B b = new B();
            b.Action();

            IPhone13ProMax i13 = new IPhone13ProMax();
            i13.Action();

            BB bb = new BB();
            bb.Action();
            bb.RemoDirnk();
            bb.Action();

            jisuan js;//声明委托变量
            Math m = new Math();
            js = new jisuan(m.add);
            //js += m.jian;
            int result = js(5, 3);
            Console.WriteLine(result);
            js += m.jian;
            int result2 = js(5, 3);
            Console.WriteLine(result2);

            DOSomethings ds = new DOSomethings(new A1().BuySomething);
            B1 b1 = new B1();
            b1.Action(ds, "方便面");
            C1 c1 = new C1();
            c1.Action(ds, "火腿肠");

        }
    }
    class A
    {//A是被委托对象
        public void Bufood()
        {
            Console.WriteLine("帮人买吃的");
        }
    }
    class B
    {//委托对象
        public delegate void DoSomething();//声明了一个委托
        DoSomething d = new DoSomething(new A().Bufood);//A是被委托对象
        public void Action()
        {
            d();
        }
    }
    class Foxconn
    {
        public void ProduceIPhone()
        {
            Console.WriteLine("我帮忙生产手机");
        }
    }
    class IPhone13ProMax
    {
        public delegate void DoIPhone();
        DoIPhone dp = new DoIPhone(new Foxconn().ProduceIPhone);
        public void Action()
        {
            dp();//通过委托去调用方法
        }
    }
    class AA
    {
        public void BuyFood(){Console.WriteLine("我帮忙买吃的");}
        public void BuyDrink() { Console.WriteLine("我帮你买喝的"); }
    }
    class BB
    {
        public delegate void DoSomething();
        private DoSomething ds;
        AA aa = new AA();
        public BB()
        {
            ds = aa.BuyFood;//赋值
            ds += aa.BuyDrink;//绑定
            //ds -= aa.BuyFood;//移除
        }
        public void Action()
        {
            ds();
        }
        public void RemoDirnk()
        {
            ds -= aa.BuyDrink;//移除
        }
    }
    
    
    public delegate int jisuan(int num1, int num2);
    class Math
    {
        public int add(int num1,int num2)
        {
            return num1 + num2;
        }
        public int jian(int num1,int num2)
        {
            return num1 - num2;
        }
    }


    public delegate void DOSomethings(string name);
    class A1
    {
        public void BuySomething(string name)
        {
            Console.WriteLine("我要帮忙买{0}", name);
        }
    }
    class B1
    {
        public void Action(DOSomethings d1,string fbm)
        {
            d1(fbm);
        }
    }
    class C1
    {
        public void Action(DOSomethings d2,string htc)
        {
            d2(htc);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C# 委托(Delegate)是一种类型,它可以用于封装一个或多个方法,使其可以像其他对象一样传递、存储和调用。委托在事件处理、多线程等方面有着广泛的应用。 C# 委托的声明方式与方法类似,可以带有参数和返回值类型,例如: ```csharp public delegate int Calculate(int a, int b); ``` 上面的代码声明了一个名为 `Calculate` 的委托类型,它包含两个 `int` 类型的参数并返回一个 `int` 类型的值。接下来可以使用这个委托类型来封装一个或多个方法。 委托的使用步骤如下: 1. 声明委托类型 ```csharp public delegate void MyDelegate(string message); ``` 2. 定义委托变量 ```csharp MyDelegate myDelegate; ``` 3. 实例化委托变量 ```csharp myDelegate = new MyDelegate(MethodA); ``` 4. 调用委托 ```csharp myDelegate("Hello"); ``` 完整的示例代码如下: ```csharp using System; namespace DelegateDemo { public delegate void MyDelegate(string message); class Program { static void Main(string[] args) { MyDelegate myDelegate; myDelegate = new MyDelegate(MethodA); myDelegate += new MyDelegate(MethodB); myDelegate("Hello"); } static void MethodA(string message) { Console.WriteLine("MethodA: " + message); } static void MethodB(string message) { Console.WriteLine("MethodB: " + message); } } } ``` 上面的代码定义了一个名为 `MyDelegate` 的委托类型,包含一个 `string` 类型的参数并返回一个 `void` 类型的值。在 `Main` 方法中,首先将 `myDelegate` 委托变量实例化为 `MethodA` 方法,然后再将其实例化为 `MethodB` 方法。最终调用 `myDelegate` 委托变量时,将会依次调用 `MethodA` 和 `MethodB` 方法。 除了以上示例中的简单委托,还有多播委托、泛型委托、匿名委托等更加高级的委托用法,可以根据具体需求选择使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值