C# 委托

定义:

委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递。事件是一种特殊的委托。

using System;
namespace App{
    public delegate int MethodDelegate(int a,int b);
    class MyClass{
        public static void Main(string[] args){
            MethodDelegate ms= new MethodDelegate((new MyClass()).Add);//方法作为参数
            Console.WriteLine(ms(10,20));//30
        }
        int Add(int a,int b){
            return a+b;
        }
    }
}

 委托与匿名函数,lambda表达式写法

using System;
namespace App {
    delegate void testDel (string a);
    class MyClass {
        public static void Main (string[] args) {
            testDel testAdd1 = new testDel (add);
            testDel testAdd2 = delegate (string a) {
                Console.WriteLine (a);
            };
            testDel testAdd3 = (a) => {
                Console.WriteLine (a);
            };
            testAdd1 ("a");
            testAdd2 ("b");
            testAdd2 ("c");
        }
        static void add (string a) {
            Console.WriteLine (a);
        }
    }
}

 

 委托的泛型用法:

 一,Func<T, TResult>委托

 

using System;
namespace App {
    delegate Tresult Add<Targs, Tresult> (Targs x);
    class MyClass {
        public static void Main (string[] args) {
            Add<int, int> add = x => x * x;
            Add<int, int> edit = delegate (int x) {
                return x * x * x;
            };
            Console.WriteLine (add (1000));
            Console.WriteLine (edit (3));
        }
    }
}

  Func委托的返回值不能为void

using System;
namespace App{
    delegate Tresult Del<Targs,Tresult>(Targs a); 
    class MyClass{
        public static void Main(string[] args){
            Del<string,void> del2 = delegate (string x){ //返回值不能为void
                Console.WriteLine(x+"\nHello World!");
            };
        }
    }
}

 3、多参数Func<T>委托

using System;
namespace App{
    class MyClass{
        public static void Main(string[] args){
            Func<int,int ,int>  add=(x,y)=>x*y;
            Console.WriteLine(add(12,13));
        }
    }
}

 4,无参数,有返回值的Func<T>泛型委托示例:

using System;
namespace App{
    class MyClass{
        public static void Main(string[] args){
            //Action add =()=>{Console.WriteLine("aaa");};
            //add();
            Func<int> add =()=>13*15;//返回值为int
            Console.WriteLine(add());//195
        }
    }
}

 

二,Action<T>委托

using System;
namespace Name {
    delegate void Action<T> (T a);
    class MyClass {
        public static void Main (string[] args) {
            Action<int> add = delegate (int a) {
                Console.WriteLine (a);
            };
            add (10);
        }
    }
}
  • 如果要委托的方法没有参数也没有返回值就想到Action
  • 有参数但没有返回值就想到Action<T>
  • 无参数有返回值、有参数且有返回值就想到Func<T>
  • 有bool类型的返回值,多用在比较器的方法,要委托这个方法就想到用Predicate<T>

 

using System;
namespace App{
    class MyClass{
        public static void Main(string[] args){
            Action<int> add =(x)=>{Console.WriteLine(x*x);};
            add(12);
        }
    }
}

 Action<T> 委托的参数不能为空

using System;
namespace App{
    class MyClass{
        public static void Main(string[] args){
            Action<void> add =()=>{Console.WriteLine("aaa");};//错误的示例
            add();
        }
    }
}

Action 无参数无返回值 示例

using System;
namespace App{
    class MyClass{
        public static void Main(string[] args){
            Action add =()=>{Console.WriteLine("aaa");};
            add();
        }
    }
}

 

posted @ 2019-04-17 09:14  liliyou  阅读(142)  评论(0编辑  收藏  举报