C#委托

delegate void MyDel(int value);
    class Program
    {
        void PrintLow(int value)
        {
            Console.WriteLine("{0}-Low Value", value);
        }
        void PrintHight(int value)
        {
            Console.WriteLine("{0}-High Value",value);
        }
        static void Main(string[] args)
        {
            Program program = new Program();
            MyDel del; //声明委托变量
            Random rand = new Random();
            int randomValue = rand.Next(99);
            //创建委托对象,并赋值给del变量
            del = randomValue < 50
                ? new MyDel(program.PrintLow)
                : new MyDel(program.PrintHight);
            del(randomValue);//执行委托
        }
    }

  委托和类一样,是用户自定义的一种类型,表示的是数据和方法的集合,持有一个或多个方法,和一系列预定义操作

1.声明一个委托类型

2.使用该委托类型声明一个委托变量

3.创建委托类型的对象,把它赋值给委托变量。

4.代码中可以像调用方法一样调用委托

委托声明和方法声明的不同:

1.以delegate管家你子开头

2.没有方法主体

二:创建委托对象

委托是引用类型,因此有引用和对象

1.new运算符操作数组成如下

(1)委托类型名

(2)一组圆括号,包含作为调用列表中第一个成员的方法的名字,方法可以是实例方法或静态方法。

delVar=new MyDel(myInsObj.My)

  快捷语法

delVar=myInsObj.My

  定义一个没有返回值和参数类型对策委托

 delegate void PrintFunction();
    class Test
    {
        public void Print1()
        {
            Console.WriteLine("Print1---instance");
        }
        public static void Print2()
        {
            Console.WriteLine("Print2---static");
        }
    }
static void Main(string[] args)
        {
            Test t = new Test();
            PrintFunction pf;//创建一个空委托
            pf = t.Print1;//实例化并初始化该委托
            //给委托增加3个另外的方法
            pf += Test.Print2;
            pf += t.Print1;
            pf += Test.Print2;

            if (null != pf) pf();  //调用委托
            else
                Console.WriteLine("Delegate is empty");
            Console.ReadLine();
        }
           //Print1-- - instance
            //Print2-- -static
            //Print1---instance
            //Print2-- -static

  调用带返回值得委托

delegate int MyDel();
    class MyClass
    {
        int intValue = 5;
        public int Add2() { intValue += 2;return intValue; }
        public int Add3() { intValue += 3;return intValue; }
    }
class Program
    {
        //调用列表中最后一个方法返回的 值就是委托调用返回的值
        //调用列表中所有其他方法的返回值都会被忽略
        static void Main(string[] args)
        {
            MyClass mc = new MyClass();
            MyDel mDel = mc.Add2;//创建并初始化委托
            mDel += mc.Add3;
            mDel += mc.Add2;
            Console.WriteLine("Value:{0}",mDel());

            Console.ReadLine();
        }
    }

  调用带引用参数的委托

delegate void MyDel(ref int x);
    class MyClass
    {
        public void Add2(ref int x) { x += 2; }
        public void Add3(ref int x) { x += 3; }
    }
//有引用参数,参数值会根据调用列表中的一个或多个方法的返回值而改变
    //在调用委托列表中的下一个方法时,参数的新值(不是初始值)会传递给下一个方法
    class Program
    {
        static void Main(string[] args)
        {
            MyClass mc = new MyClass();

            MyDel mDel = mc.Add2;
            mDel += mc.Add3;
            mDel += mc.Add2;

            int x = 5;
            mDel(ref x);
            Console.WriteLine("Value:{0}",x);
            Console.ReadLine();
        }
    }

  匿名方法

是在初始化委托时内联声明的方法

 delegate int otherDel(int inParam);
    class Program
    {
        static void Main(string[] args)
        {
            otherDel del = delegate (int x)
            {
                return x += 20;
            };
            Console.WriteLine("{0}",del(5));
            Console.WriteLine("{0}",del(6));
            Console.ReadLine();
        }
    }

  使用匿名方法的地方:

1.delegate类型关键字

2.参数列表,如果语句块没有使用任何参数可以省略

3.语句块,包含了匿名方法的代码

Lambda表达式

1.删除delegate关键字

2.在参数列表和匿名方法主体之间放入Lambda运算符=>

 delegate double MyDel(int par);
    class Program
    {
        static void Main(string[] args)
        {
            MyDel del = delegate (int x) { return x + 1; };//匿名方法

            MyDel le1 = (int x) => { return x + 1; };//Lambda 表达式
            MyDel le2 = (x) => { return x + 1; };
            MyDel le3 = x => { return x + 1; };
            MyDel le4 = x => x + 1;

            Console.WriteLine("{0}",del(12));
            Console.WriteLine("{0}", le1(12));
            Console.WriteLine("{0}", le2(12));
            Console.WriteLine("{0}", le3(12));
            Console.WriteLine("{0}", le4(12));

            Console.ReadLine();
        }
    }

  Lambda表达式的参数列表的要点如下:

1.:Lambda表达式参数列表中的参数必须在参数数量、类型和位置上与委托相匹配

2.表达式的参数列表中的参数不一定需要包含类型(隐式类型)除非委托有ref 或者out 参数

原文地址:https://www.cnblogs.com/sunliyuan/p/9852872.html