C# 委托详解

https://blog.csdn.net/life_is_crazy/article/details/78206166?spm=1001.2101.3001.6661.1&utm_medium=distribute.pc_relevant_t0.none-task-blog-2~default~BlogCommendFromBaidu~default-1-78206166-blog-115975249.pc_relevant_vip_default&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-2~default~BlogCommendFromBaidu~default-1-78206166-blog-115975249.pc_relevant_vip_default&utm_relevant_index=1

1:将方法作为方法的参数

委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递,这种将方法动态地赋给参数的做法,可以避免在程序中大量使用If-Else(Switch)语句,同时使得程序具有更好的可扩展性。

namespace TestWt
{
	//定义委托
	public delegate void GreetingDelegate(string name);
	class Program
	{
		//注意此方法,它接受一个GreetingDelegate类型的方法作为参数
		private static void GreetPeople(string name, GreetingDelegate MakeGreeting)
		{
			MakeGreeting(name); 
		}

		private static void EnglishGreeting(string name)
		{
			Console.WriteLine("Morning, " + name);
		}
		private static void ChineseGreeting(string name)
		{
			Console.WriteLine("早上好, " + name);
		}

		static void Main(string[] args)
		{
			GreetPeople("Jimmy Zhang", EnglishGreeting);
			GreetPeople("张子阳", ChineseGreeting);
			Console.ReadKey();
		}
	}
}

将方法绑定到委托

使用委托可以将多个方法绑定到同一个委托变量,当调用此变量时(这里用“调用”这个词,是因为此变量代表一个方法),可以依次调用所有绑定的方法。

namespace TestWt
{
	//定义委托
	public delegate void GreetingDelegate(string name);
	
	class Program
	{
		//注意此方法,它接受一个GreetingDelegate类型的方法作为参数
		private static void GreetPeople(string name, GreetingDelegate MakeGreeting)
		{
			MakeGreeting(name); 
		}

		private static void EnglishGreeting(string name)
		{
			Console.WriteLine("Morning, " + name);
		}
		private static void ChineseGreeting(string name)
		{
			Console.WriteLine("早上好, " + name);
		}

		static void Main(string[] args)
		{
			//GreetPeople("Jimmy Zhang", EnglishGreeting);
			//GreetPeople("张子阳", ChineseGreeting);
			//Console.ReadKey();

			GreetingDelegate delegate1, delegate2;
			delegate1 = EnglishGreeting;
			delegate2 = ChineseGreeting;

			GreetPeople("Jimmy Zhang", delegate1);
			GreetPeople("张子阳", delegate2);
			Console.ReadKey();
		}
	}
}


   static void Main(string[] args)
    {
        //GreetPeople("Jimmy Zhang", EnglishGreeting);
        //GreetPeople("张子阳", ChineseGreeting);
        //Console.ReadKey();

        //GreetingDelegate delegate1, delegate2;
        //delegate1 = EnglishGreeting;
        //delegate2 = ChineseGreeting;

        //GreetPeople("Jimmy Zhang", delegate1);
        //GreetPeople("张子阳", delegate2);
        //Console.ReadKey();


        GreetingDelegate delegate1;
        delegate1 = EnglishGreeting; // 先给委托类型的变量赋值
        delegate1 += ChineseGreeting;   // 给此委托变量再绑定一个方法

        // 将先后调用 EnglishGreeting 与 ChineseGreeting 方法
        GreetPeople("Jimmy Zhang", delegate1);
        Console.ReadKey();
    }
	public void GreetPeople(string name) 
	{
		if(delegate1!=null)
		{     //如果有方法注册委托变量
		  delegate1(name);      //通过委托调用方法
		}
	}
	
	
	
	static void Main(string[] args)
    {
        GreetingDelegate delegate1 = new GreetingDelegate(EnglishGreeting);
        delegate1 += ChineseGreeting;   // 给此委托变量再绑定一个方法

        // 将先后调用 EnglishGreeting 与 ChineseGreeting 方法
        GreetPeople("Jimmy Zhang", delegate1);
        Console.WriteLine();

        delegate1 -= EnglishGreeting; //取消对EnglishGreeting方法的绑定
                                      // 将仅调用 ChineseGreeting
        GreetPeople("张子阳", delegate1);
        Console.ReadKey();
    }

事件的由来

Event出场了,它封装了委托类型的变量,使得:在类的内部,不管你声明它是public还是protected,它总是private的。在类的外部,注册“+=”和注销“-=”的访问限定符与你在声明事件时使用的访问符相同。

namespace TestWt
{
	//定义委托
	public delegate void GreetingDelegate(string name);
	//新建的GreetingManager类
	public class GreetingManager
	{
		//这一次我们在这里声明一个事件
		public event GreetingDelegate MakeGreet;
		public void GreetPeople(string name)
		{
			MakeGreet(name);
		}
	}
}
namespace TestWt
{
	class Program
	{
		private static void EnglishGreeting(string name)
		{
			Console.WriteLine("Morning, " + name);
		}

		private static void ChineseGreeting(string name)
		{
			Console.WriteLine("早上好, " + name);
		}

		static void Main(string[] args)
		{
			GreetingManager gm = new GreetingManager();
			gm.MakeGreet += EnglishGreeting;         
			gm.MakeGreet += ChineseGreeting;

			gm.GreetPeople("Jimmy Zhang");

			Console.ReadKey();
		}
	}
}
posted @ 2022-06-30 17:49  mkmkbj  阅读(163)  评论(0编辑  收藏  举报