unity游戏开发-C#语言基础篇(多种委托)

  class Program
    {
        public delegate void mydelegate();
        public event mydelegate myevent;

        public delegate void mydelegate(int a);//带参
        public event mydelegate myevent;
        static void Main(string[] args)
        {
             //多种委托
            Action a = Test1;
            a += Test2;
            a += Test3;
            
           Delegate[] d=a.GetInvocationList();

           foreach (Delegate item in d)
           {
               Console.WriteLine(item.DynamicInvoke());
           }




           Func b = Test4;
           b += Test5;
           b += Test6;

           Delegate[] d1 = b.GetInvocationList();

           foreach (var item in d1)
           {
               Console.WriteLine(item.DynamicInvoke());
           }




           // 匿名方法
            Action
            Action a = delegate()
            {
                Console.WriteLine("Hello World!");

            };//匿名方法注意分号;
            a();

            Action b = delegate(int aa)
            {

                Console.WriteLine("{0}",aa);
            };

            b(88);

            Function

            Func f = delegate()
            {
                Console.WriteLine("123");
                return "123";
            };
            f();

            Func ff = delegate(string f1)
            {
                Console.WriteLine("{0}",f1);
                return "123";
            };
            ff("8888");


            string a111 = "1";
            string a222 = "";

            string c = Show("1", out a222);zai fang
            Console.WriteLine(c);
            Console.WriteLine(a222); 

            Func f = delegate()
            {


                return true;
            };
            bool b = f();
            Console.WriteLine(b);

            Func  f1=delegate(int a,string str){

               
                Console.WriteLine("{0}   {1}",a,str);
                return false;
            
            };
            bool bb = f1(1,"88");
            Console.WriteLine(bb);


           // 事件

            Program p = new Program();
            p.myevent = Test1;
            p.myevent();



            Program p = new Program();

            p.myevent = Test2;
            p.myevent(8);





           Console.ReadKey();

        }


        static void Test1()
        {
            Console.WriteLine("Test1");
        }




        static void Test2(int a)
        {
            Console.WriteLine("Test2  {0}", a);
        }

        static void Test3()
        {
            Console.WriteLine("Test3");
        }




        static string Test4()
        {
            Console.WriteLine("Test1-f");
            return "1";
        }

        static string Test5()
        {
            Console.WriteLine("Test2-f");
            return "2";
        }
        static string Test6()
        {
            Console.WriteLine("Test3-f");
            return "3";
        }



        public static string Show(string a, out string b)
        {
            b = "123";
            return a;
        }

    }

你可能感兴趣的:(unity游戏开发-C#语言基础篇(多种委托))