C# 委托案例

1、委托是将方法作为方法的参数。

案例1:常规多态实现

using System;

namespace ConsoleApp1
{  
     class Program
    {
        public enum Language { 
        English,Chinese
        }
       
        public static void SayHello(string name, Language lang)
        {
            switch (lang) {
                case Language.English:
                    EnglishGreeting(name);
                    break;
                case Language.Chinese:
                    ChineseGreeting(name);
                    break;
            }
            
        }
        public static  void EnglishGreeting(string name)
        {
            Console.WriteLine("hello, "+name);
        }
        public static void ChineseGreeting( string name)
        {
            Console.WriteLine("你好," + name);
        }

        static void Main(string[] args)
        {
            SayHello("laola", Language.English);
            SayHello("劳拉", Language.Chinese);
        }
    }
   
}




案例1改进:使用委托

using System;

namespace ConsoleApp1
{  
     class Program
    {
        public delegate void GreetingDelegate(string name);
        //GreetingDelegate可以背用于引用任何一个有单一string 类型参数的方法,并返回void类型的变量。
        public static void SayHello(string name, GreetingDelegate greetPeople)
        {
            greetPeople(name);
        }
        public static  void EnglishGreeting(string name)
        {
            Console.WriteLine("hello, "+name);
        }
        public static void ChineseGreeting( string name)
        {
            Console.WriteLine("你好," + name);
        }

        static void Main(string[] args)
        {
            SayHello("laola", EnglishGreeting);
            SayHello("劳拉", ChineseGreeting);

            Type t = typeof(GreetingDelegate);
            Console.WriteLine(t.IsClass);//委托是类,避免在程序中大量使用if-else或switch语句,同时使得程序更好的可拓展性。

        }
    }
   
}

在这里插入图片描述

2、实例化委托和内置委托

委托对象可以通过new关键字创建,且与一个特定的方法有关。
当创建委托时,传递到new语句的参数和方法调用一样写,但不带有参数。

//用c++实现同功能
#include<iostream>
using namespace std;
typedef int(*func1)(int x, int y);
int Add(int x, int y) {
	return x + y;
}
int Sub(int x, int y) {
	return x - y;
}
int main() {
	int x = 100, y = 200;
	int z = 0;
	func1 fun = &Add;
	z = fun(x, y);
	printf("%d+%d=%d\n",x,y,z);
	fun = &Sub;
	z = fun(x, y);
	printf("%d-%d=%d", x, y, z);
}

一下图片来自中国慕课MOOC
在这里插入图片描述
在这里插入图片描述

案例:

using System;
//委托时函数指针的升级版
namespace ConsoleApp1
{  
    class Calculate
    {
        public int Add(int x,int y)
        {
            return x + y;
        }
        public int Sub(int x,int y)
        {
            return x - y;
        }
    }
     class Program
    {
        static void Main(string[] args)
        {
            var g=new Greeting();
            Action action1 = new Action(g.SayHello);
            action1();
            Action<string> action2 = new Action<string>(g.SayHelloToSomeon);
            action2("George");

            Calculate c = new Calculate();
            Func<int, int, int> func1 = new Func<int, int, int>(c.Add);
            Console.WriteLine(func1(1,2));
            Func<int, int, int> func2 = new Func<int, int, int>(c.Sub);
            Console.WriteLine(func2(2, 1));
            Console.ReadLine();
        }
    }
   
    class Greeting
    {
        public void SayHello()
        {
            Console.WriteLine("hello ,guys");
        }

        public void SayHelloToSomeon(string name)
        {
            Console.WriteLine("hello ,"+name);
        }
    }

}

在这里插入图片描述

3、多播委托

using System;
//委托时函数指针的升级版
namespace ConsoleApp1
{  
     class Program
    {
        static void Main(string[] args)
        {
            Draw d1 = new Draw() { StuId = 1, PenColor = ConsoleColor.Red,Age=11 };
            Draw d2 = new Draw() { StuId = 2, PenColor = ConsoleColor.Green,Age=21 };
            Action action1 = new Action(d1.DrawPicture);
            Action action2 = new Action(d2.DrawPicture);
            //action1();
            //action2();
            action1 += action2;
            action1();//这里是多播委托:可以通过+或- 相同类型进行合并或移除

            //Func<int> fun1 = new Func<int>(d1.bornYear);
            //Console.WriteLine(fun1());
            //fun1 = new Func<int>(d2.bornYear);
            //Console.WriteLine(fun1());
            //Console.ForegroundColor = ConsoleColor.White;
        }
    }
   class Draw
    {
        public int StuId{ set; get; }
        public ConsoleColor PenColor { set; get; }
        public void DrawPicture()
        {
            Console.ForegroundColor = PenColor;
            Console.WriteLine("Student {0} draw a {1}cat", StuId,PenColor);
        }
        public int Age { set; get; }
        public int bornYear()
        {
            Console.ForegroundColor = PenColor;
            return 2020 - Age;
        }
    }
    

}




  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

广大菜鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值