C Sharp 委託方法


        //定義一個委託類型
        public delegate void Methodv();
        //一個普通方法
        public static void OnMethodv()
        {
            Console.WriteLine("On Method v");
        }
        static void Main(string[] args)
        {
            //使用委託類型創建實例(委託的參數,返回值必須和賦值的普通方法的一致)
            Methodv mv = new Methodv(OnMethodv);
            //通過委託調用普通方法
            mv();                   //輸出 On Method v
            Console.ReadKey();
        }

 


        //定義一個委託類型
        public delegate string Methods(int para1,string para2);
        //一個普通方法
        public static string OnMethods(int i,string s)
        {
            Console.WriteLine("On Method s");
            return "";
        }

        static void Main(string[] args)
        {
            //使用委託類型創建實例(委託的參數,返回值必須和賦值的普通方法的一致)
            Methods ms = OnMethods;        
            //通過委託調用普通方法
            ms.Invoke(12, "Black"); //輸出 On Method s
            Console.ReadKey();
        }

 系統內置委託Action

        //一個普通方法
        public static void OnMethodv()
        {
            Console.WriteLine("On Method v");
        }

        static void Main(string[] args)
        {
            //系統內置委託 Action: 沒有返回值,最多可以有16個參數
            Action cv = OnMethodv;
            cv();                   //輸出 On Method v

            Console.ReadKey();
        }

系統內置委託Action

        //一個普通方法
        public static void OnMethodf(int i, string s, float f)
        {
            Console.WriteLine("On Method f");
        }
        static void Main(string[] args)
        {
            //系統內置委託 Action: 沒有返回值,最多可以有16個參數
            Action<int, string, float> cs = OnMethodf;
            cs(8, "Black", 1.6f);   //輸出 On Method f

            Console.ReadKey();
        }

 系統內置委託Func

        //一個普通方法
        public static bool OnMethodb(int i, string s, float f)
        {
            Console.WriteLine("On Method b");
            return true;
        }

        static void Main(string[] args)
        {
            //系統內置委託 Func: 有返回值(最後一個泛型),最多可以有16個參數
            Func<int, string, float, bool> fb = OnMethodb;  //返回值bool 參數int,string,float
            bool res = fb.Invoke(9, "Black", 1.2f);         //輸出 On Method b

            Console.ReadKey();
        }

匿名方法

//匿名方法
Func<int, int, string> fun = delegate(int arg1, int arg2)
{
    return arg1 + "/" + arg2;
};
//測試
Console.WriteLine(fun(1, 2));       //1/2

Lambda

            //Lambda
            Func<int, int, string> fun = (int arg1, int arg2) =>
            {
                return arg1 + "/" + arg2;
            };
            //測試
            Console.WriteLine(fun(1, 2));       //1/2

            //Lambda
            //參數只有一個,不加()
            Func<int, string> fun = arg =>
            {
                return arg.ToString();
            };
            //測試
            Console.WriteLine(fun(1));       //1
            //Lambda
            //方法只有一句,不加{},return,直接返回
            Func<int, string> fun = arg => arg.ToString();
            //測試
            Console.WriteLine(fun(1));       //1

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章