c# 委托学习文档

来源:互联网 发布:西科软件实习骗局 编辑:程序博客网 时间:2024/04/20 23:15

我认为委托就是一个方法的集合队列

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace learn_4_21_12_39{    delegate void MyDel(int value);//声明委托类型    class Program    {        void PrintLow(int value)        {            Console.WriteLine("{0} - Low Value", value);        }        void PrintHigh(int value)        {            Console.WriteLine("{0} - High Value", value);        }        void PrintHelloWorld(int value)        {            Console.WriteLine("hello world!{0}", value);        }        static void Main(string[] args)        {            Program pro = new Program();            MyDel del;          //声明委托变量            Random rand = new Random();            int randomValue = rand.Next(1,100);            //创建一个包含PrintLow或PrintHigh的委托对象并将其赋值给del变量            del = randomValue < 50 ? new MyDel(pro.PrintLow) : new MyDel(pro.PrintHigh);            del += pro.PrintHelloWorld;//为委托添加方法            del(randomValue);            del -= pro.PrintHelloWorld;//从委托移除方法            del(randomValue);            Console.ReadLine();        }    }}
using System;using System.Collections.Generic;using System.Linq;using System.Text;//委托的示例namespace learn_4_21_15_25{    //定义一个委托类型    delegate void PrintFunction();    class Test    {        public void Print1()        {            Console.WriteLine("Print1 -- instance");        }        public static void Print2()        {            Console.WriteLine("Print2 -- static");        }    }    class MyClass    {        static void Main()        {            Test t = new Test();            PrintFunction pf;//创建一个空委托            pf = t.Print1;//实例化并初始化该委托            //给委托增加3个另外的方法            pf += Test.Print2;            pf += t.Print1;            pf += Test.Print2;            if (pf != null)            {                pf();            }            else            {                Console.WriteLine("Delegate is empty");            }            Console.ReadLine();        }    }}
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace learn_4_21_15_45{    //调用带返回值的委托    delegate int MyDel();    class Test    {        int IntValue = 5;        public int Add2()        {            IntValue += 2;            return IntValue;        }        public int Add3()        {            IntValue += 3;            return IntValue;        }    }    class MyClass    {        static void Main()        {            Test t = new Test();            MyDel mDel = t.Add2;//创建并初始化委托            mDel += t.Add3;            mDel += t.Add2;            Console.WriteLine("Value:{0}", mDel());            Console.ReadLine();        }    }}
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace learn_4_21_15_53{    //调用带引用参数的委托    delegate void MyDel(ref int x);    class MyClass    {        public void Add2(ref int x)        {            x += 2;        }        public void Add3(ref int x)        {            x += 3;        }        static void Main()        {            MyClass mc = new MyClass();            MyDel mDel = mc.Add2;            mDel += mc.Add3;            mDel += mc.Add2;            int x = 5;            mDel(ref x);            Console.WriteLine("x Value:{0}", x);            Console.ReadLine();        }    }}
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication1{    class Program    {        delegate int OtherDel(int x);        static void Main(string[] args)        {            //匿名方法            OtherDel del = delegate(int x)                            {                                return x + 20;                            };            Console.WriteLine("{0}", del(20));            //Lambda表达式            OtherDel del1 = y => y + 20;            Console.WriteLine("{0}", del1(20));            OtherDel del2 = y =>             {                Console.WriteLine("hello world");                return y + 20;            };            Console.WriteLine("{0}", del2(20));            string s = "hello";            Func<string,string,int> lambda = (s1,s2) =>             {                s1 += " world!";                return 20;            };            Console.WriteLine("{0}", lambda(s,s));            Console.ReadLine();        }    }}
1 0