日期:2014-06-10  浏览次数:20567 次

1.委托声明

   

 

2.委托入门实例

 

namespace ConsoleApplication1
{
    
    public delegate void methodDelegate(string str);
    class Program
    {
        static void Main(string[] args)
        {
            Student student = new Student();
            Teacher teacher = new Teacher("王老师");
            methodDelegate methodDelegate1 = new methodDelegate(student.getStudentName);
            methodDelegate1 += teacher.getTeacherName; //可以指向不同类中的方法!
            //methodDelegate1 += teacher.getClassName; 指向签名不符的方法时提示错误!
            methodDelegate1.Invoke("张三");
            Console.ReadLine();
        }
    }

    class Student
    {
        private String name = "";
        public Student (String _name)
        {
            this.name = _name ;
        }
        public Student() {}
        public void getStudentName(String _name) 
        {
            if (this.name != "" )
                Console.WriteLine("Student's name is {0}", this.name);
            else
                Console.WriteLine("Student's name is {0}", _name);
        }
    }

    class Teacher
    {
        private String name;
        public Teacher(String _name)
        {
            this.name = _name;
        }
        public void getTeacherName(String _name)
        {
            if (this.name != "")
                Console.WriteLine("Teacher's name is {0}", this.name);
            else
                Console.WriteLine("Teacher's name is {0}", _name);
        }
        public string getClassName() 
        {
            return "Eanlish";
        }
    }
}

上面代码中实现对委托的调用

最后将被调用的委托输出

3.委托的实现方式

  第一种:常规实现

  

private delegate String getAString( string parament);
        static void Main(String []args)
        {
            int temp = 40;
            getAString stringMethod = new getAString(temp.ToString); //传递temp.ToString调用委托
Console.WriteLine("String is {0}", stringMethod()); //stringMethod()调用已经接受参数的委托 Console.ReadLine(); }

 第二种:多播委托

 getAString stringMethod = new getAString(temp.ToString);
         stringMethod += temp.ToString;
         stringMethod -= temp.ToString;  

  这种调用之后,按照接受参数的次数, 委托会生成一个列表,每用+=调用一次,会增加一个列表项,-=调用一次,会移除一个列表项。

 第三种:委托数组

    当我们定义委托 让委托形成一个数组的时候,我们可以通过遍历数组的方式来调用它

   

delegate double Operations(double x);

    class Program
    {
        static void Main()
        {
            Operations[] operations =
            {
               MathOperations.MultiplyByTwo,
               MathOperations.Square
            };

            for (int i = 0; i < operations.Length; i++)
            {
                Console.WriteLine("Using operations[{0}]:", i);
                DisplayNumber(operations[i], 2.0);
                DisplayNumber(operations[i], 7.94);
                Console.ReadLine();
            }
        }

        static void DisplayNumber(Operations action, double value)
        {
            double result = action(value);
            Console.WriteLine(
               "Input Value is {0}, result of operation is {1}", value, result);
        }
    }

    struct MathOperations
    {
        public static double MultiplyByTwo(double value)
        {
            return value * 2;
        }

        public static double Square(double value)
        {
            return value * value;
        }
    }

上面实例中

 将委托定义好

之后就可以遍历它

第四种:Action<T>和Func<T>委托

利用微软预先定义的两个泛型类型来给我们调用

  先看语法知识

  Action<T>

  

Func<T>

二者都可以接受最大参数个数为16个,二者的区别在于一个有返回值,另外一个返回值为void

实例

using System;
namespace DelegateFuncAction
{
    class Program
    {
        static void Main(string[] args)
        {
            Func<double, double,double> DoAddtion = calculate.addtion;
            double result = DoAddtion(20, 30);
            Console.WriteLine("Func带返回参数委托做加法结果为:{0}",DoAddtion(10,20));
            calculate c=new calculate();
            Action<double, double> DoSubstraction = c.substraction;
            DoSubstraction(90, 20);
        }
    }
    class calculate
    {
        public static double addtion(double x, double y)
        {
            return x + y;
        }
        public void substraction(double x, double y)
        {
            Console.WriteLine("Action不带返回参数委托做减法结果为:{0}",x-y);
        }
    }
}

二者定义的方法名都可以把函数赋予它 如下

 

第五种:匿名方法

   匿名方法基本上和上面没有区别,只是实例化时候不同而已

  

class Program
    {
        static void Main()
        {
            double width = 12;

            Func<double, double> square = delegate(double length)  
            {
                length *= width;
                return length;
            };
            Console.WriteLine(square(10));
        }
    }

这样减少了代码的编写  将一个函数直接在定义的时候赋予Func<T>或者Action<T>定义的函数

第六种:Lambda表达式

   先看Lambda表达式的语法知识

 

例如

delegate int del(int i);
static void Main(string[] args)
{
    del myDelegate = x => x * x;
    int j = myDelegate(5); //j = 25
}

 

  实例中

 x => x * x就是一个参数为x  执行x*x的lambda的表达式  赋予一个委托

4.总结

  在声明委托时候可以用三种方式来声明

   1.用 delegate关键词来声明(需要声明返回值,接受参数)

   2.Action<T>来声明一个返回值为void类型的委托

   3.Func<T>来声明有返回类型的委托

   委托的实现

 无论委托是如何实现的,我们都是赋予它函数时候体现的