C# 委托的发展,变化
含义:委托是一种在对象里保存方法引用的类型,同时也是一种类型安全的函数指针。
格式:类似方法的定义,但没有方法体,定义的委托名前要加上关键字delegate。
作用域:可以在定义类的任何地方定义委托,既可以在另一个类的内部定义委托,也可以在所有类的外部定义委托,还可以在命名空间中把委托定义为顶层对象。根据定义的可见性,可以在委托定义上添加一般的访问修饰符:public、private和protected等
优势:1、简化调用方法。2、提升应用程序的性能
下面显示委托在各版本中的不同写法:
1、vs2003, 对应的C# 1.1
delegate int HandlerAdd(int a, int b);
        static int NumberAdd(int a, int b)
        {
            int num = a + b;
            Console.WriteLine(num);
            return num;
        }
        static void Main(string[] args)
        {
            HandlerAdd operate = new HandlerAdd(NumberAdd);
            operate(10,20);
        }
delegate int HandlerAdd(int a, int b);
        static void Main(string[] args)
        {
            //匿名委托
            HandlerAdd operate = delegate(int a, int b) { return a + b; };
            int num = operate(10,20);
            Console.WriteLine(num);
        }
3、vs2008,对应的c#3.5版本(3.0版本就已经有了),拉姆达(lamaba expression)表达式的引入,赋予委托极具变化的特征
        static void Main(string[] args)
        {
            HandlerAdd operate = (a, b) => { return a + b; };
            int num = operate(10,20);
            Console.WriteLine(num);
        }
{
public delegate TResult Func<TResult>();
public delegate TResult Func<T, TResult>(T arg);
public delegate TResult Func<T1, T2, TResult>(T1 arg1, T2 arg2);
public delegate TResult Func<T1, T2, T3 TResult>(T1 arg1, T2 arg2, T3 arg2);
public delegate TResult Func<T1, T2, T3, T4, TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4);
}
        static void Main(string[] args)
        {
            int number = 0;
            Func<int> function1 = () => { return 1; };
            Func<int,int> function2 = (int a) => { return a; };
            Func<int,int,int> function3 = (int a,int b) => { return a + b; };
            Func<int,int,int,int> function4 = (int a,int b,int c) => { return a + b+ c; };
            Func<int,int,int,int,int> function5 = (int a,int b,int c,int e) => { return a + b+ c + e; };
            number = function1();
            number = function2(1);
            number = function3(1,2);
            number = function4(1,2,3);
            number = function5(1,2,3,4);
            //这些写法都是相同的
            Func<int, int, int, int, int> function6 = Count;
        }
        public static int Count(int a, int b, int c, int d)
        {
            return a + b + c + d;
        }
        static void Main(string[] args)
        {
            //定义一个委托 FindValue 判断字符串中是否有包括 "a" 这个值
            Func<string, bool> FindValue = (string a) => { return a.IndexOf("a") != -1 ; };
            //构造一个数组
            List<string> arr = new List<string>();
            arr.Add("aaaa");
            arr.Add("bbbb");
            //把 FindValue 传递到 Where 方法中
            var result = arr.Where(FindValue).ToList();
            Console.WriteLine(result.Count); //值是 1
        }
从委托的发展,我们可以看出c#是越来越美,编写的代码也是越来越少。所以有人说:下一代编程语言最有可能会从C#中产生。
 
                    
                     
                    
                 
                    
                
 
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号