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<string> 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<int> b = delegate(int aa)
            {

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

            b(88);

            Function

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

            Func<string, string> 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<bool> f = delegate()
            {


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

            Func <int,string,bool> 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;
        }

    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容