いくつかの依頼の使い方Action,Func,匿名,マルチキャスト,Lambde表現,eventを整理する

8256 ワード

1.常用委託形式:
例:
              public static void Main (string[] args)
		{
			Meet ("China");
			Meet ("Japan");
		}

		public static void SayHelloInChina()
		{
			Console.WriteLine ("   ?");
		}
		public static void SayHelloInAmerica()	
		{
			Console.WriteLine ("what is the weather?");
		}
		public static void SayHelloInJapan()
		{
			Console.WriteLine ("    !");
		}
		public static void SayHelloInHanGuo()
		{
			Console.WriteLine ("……   ");
		}
		public static void SayHelloInAnyCountry(){}


		public static void Meet(string s)
		{
			if (s == "China") 
			{
				SayHelloInChina ();
			}
			if (s == "America" ) 
			{
				SayHelloInAmerica ();
			}
			if (s == "Japan") 
			{
				SayHelloInJapan ();
			}
			if (s == "HanGuo") {
				SayHelloInHanGuo ();
			}
		}
          public static void Main (string[] args)
		{
			//         
			//                  
			Meet(SayHelloInChina);	//               ,       
			Meet (SayHelloInHanGuo);

		}

		public static void SayHelloInChina()
		{
			Console.WriteLine ("   ?");
		}
		public static void SayHelloInAmerica()	
		{
			Console.WriteLine ("what is the weather?");
		}
		public static void SayHelloInJapan()
		{
			Console.WriteLine ("    !");
		}
		public static void SayHelloInHanGuo()
		{
			Console.WriteLine ("……   ");
		}
		public static void SayHelloInAnyCountry(){}


		public static void Meet (SayHello say)//      
		{
			say ();	//       
		}
2.
        public delegate void kid();	//         
	class MainClass
	{
		public static void Main (string[] args)
		{
			//      
			Boss boss = new Boss ();
			//          
			kid kid = new kid (boss.Robe);
			kid += boss.Kill;	//   ,
			kid += boss.Fire;

			//       
			kid -= boss.Kill;
			kid -= boss.Fire;
			kid -= boss.Robe;

			//        ,               ,         
			if (kid != null) 
			{
				kid ();	//    
			}

		}
	}

	//     
	public class Boss
	{
		public void Fire(){
			Console.WriteLine ("  ……");
		}
		public void Kill()
		{
			Console.WriteLine ("  ……");
		}
		public void Robe()
		{
			Console.WriteLine ("   ,     ……");
		}
	}

3.
        /// 
	///       
	/// 
	public delegate double DlgtCounter (float a,float b);

	class MainClass
	{
		public static void Main (string[] args)
		{
			
			Count ct = new Count ();
//			DlgtCounter dcer = new DlgtCounter(ct.Add);
			Accountant ac = new Accountant ();
//			Console.WriteLine (ac.Operation (dcer, 4f, 5f));
			Console.WriteLine (ac.Operation (ct.Add, 3f, 5f));
			Console.WriteLine (ac.Operation (ct.Sub, 3f, 5f));
			Console.WriteLine (ac.Operation (ct.Mul, 3f, 5f));
			Console.WriteLine (ac.Operation (ct.Divide, 3f, 5f));

		}
	}

	/// 
	///    ,        
	/// 
	public class Accountant
	{
		//     (  )   
		public double Operation(DlgtCounter counter,float a, float b)
		{
			return counter (a, b);

		}
	}

	/// 
	///    ,         fgfcr0 
	/// 
	public class Count
	{
		public double Add(float a,float b)
		{
			return a + b;
		}
		public double Sub(float a,float b)
		{
			return a - b;
		}
		public double Mul(float a,float b)
		{
			return a * b;
		}
		public double Divide(float a,float b)
		{
			return a / b;
		}
	}
4.Action
             public static void Main (string[] args)
		{

			//Action         Action            
			//         Action          (      )
			Action a = PrintHelloWorld;
			a ();

			Action b = PrintString;
			b ("shenmoyisi");

			Action c = PrintDoubleInt;
			c (2, 4);

			Action d = Print;
			d (1, 2, "   ", false);
		}

		public static void PrintHelloWorld()
		{
			Console.WriteLine ("Hello World.");
		}
		public static void PrintString(string str)
		{
			Console.WriteLine (str);
		}
		public static void PrintDoubleInt(int a, int b)
		{
			Console.WriteLine (a+ " "+ b);
		}
		public static void Print(int a, int b, string c, bool d)
		{
			Console.WriteLine (a+ " " + b+ " " +c+ " " + d);
		}
5.Func
//Func           ,
	//Func        ,<>          ,          
	//                , 0~16   
	class MainClass
	{
		public static void Main (string[] args)
		{
			Func a = Test01;
			Console.WriteLine (a ());
			Func b = Test02;
			Console.WriteLine (b ("   "));
			Func c = Test03;
			int res = c (12,45);
			Console.WriteLine (res);
		}

		public static int Test01(){
			return 1;
		}
		public static int Test02(string str)
		{
			return 2;
		}
		public static int Test03(int a,int b)
		{
			return a + b;
		}
	}

6.
class MainClass
	{
		public static void Main (string[] args)
		{
			Action a = Test01;
			a += Test02;

//			a -= Test01;	//    
//			a -= Test02;
			if (a != null) {
				a ();
			}

			//     ,                       
			//        ,      (   )              
			//          ,       void
			//                
			//                    ,        。

			//        。                
			Delegate[] dlgts = a.GetInvocationList();

			//      
			foreach (Delegate i in dlgts) {
				//    
				i.DynamicInvoke ();
			}
		}
		public static void Test01()
		{
			Console.WriteLine ("Test01");
//			throw new Exception ();		//      ,    ,    ,   ,            

		}
		public static void Test02()
		{
			Console.WriteLine ("Test02");
		}
	}

7.
class MainClass
	{
		public static int Plus(int a,int b)
		{
			return a + b;
		}

		public static void Main (string[] args)
		{
			//       
			Func f1 = Plus;
			int res1 = f1(12, 34);
			Console.WriteLine ("  f1  : "+res1);

			//      ,        ,      ,        
			//                       
			Func f2 = delegate (int a, int b) {
				return a + b;
			};
			int res2 = f2 (23, 45);
			Console.WriteLine ("  f2  : " +res2);

			Func f3 = delegate(int a, int b) {
				return a - b;
			};
				int qwe = f3(4,2);
				
			

		}
	}

8. Lambda
public static void Main (string[] args)
		{

			//    
			Func f2 = delegate (int a, int b) {
				return a + b;
			};

			//Lambda   ,        ,             
			//Lambda                
			Func f3 = (arg1, arg2) => {
				return arg1 + arg2;
			};

//			Funcf3 = (arg1, arg2) => arg1 + arg2;

			Console.WriteLine (f3(12,12));

			//Lambda                    
			//             ,        return;
			//            
			Funcf4 = arg => 456;
			Console.WriteLine ("f4: " + f4(123));

			//                       
			//              Lambda   
			//Lambda   ,          

			Func f5 = arg => ++arg;
			Console.WriteLine ("f5 : " + f5(13));
			Func  f6 = asdf => 1234;
			int azxc = f6 ("   ");

		}
	}

9.event
public delegate void MyDelegate();
	class MainClass
	{
		//           
//		public MyDelegate myDelegate;
		//      ,       ,      ,
		public event MyDelegate myDelegate;

		public static void Main (string[] args)
		{
			MainClass mc = new MainClass ();
			mc.myDelegate = Test;
			mc.myDelegate ();

		}
		public static void Test()
		{
			Console.WriteLine ("Test");
		}


	}
	public delegate void MyDelegete();
	class Test01
	{
		public event MyDelegate MyDelegete01;
		public static void Main01(string[] args01){
		
			Test01 t01 = new Test01 ();
			t01.MyDelegete01 = Test02;
			t01.MyDelegete01 ();
		}
		public static void Test02()
		{
			Console.WriteLine ("   ");
		}

	}