委托和匿名方法的使用

来源:互联网 发布:itnc530模拟软件下载 编辑:程序博客网 时间:2024/05/16 01:33

1、多委托

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace MulticastDelegateInvocationList{    public delegate int MyDelegate(int value);    public class MyClass    {        public int Func1(int argument)        {            Console.WriteLine("Func1: i={0}", argument);            return argument;        }        public int Func2(int argument)        {            argument *= 2;            Console.WriteLine("Func2: i={0}", argument);            return argument;        }    }    class Program    {        static void Main(string[] args)        {            MyClass obj = new MyClass();            MyDelegate del1 = new MyDelegate(obj.Func1);            del1 += new MyDelegate(obj.Func2);            //上面两句可以简写为以下形式:            //MyDelegate del1 = obj.Func1;            //del1 += obj.Func2;            Delegate[] ds;            ds = del1.GetInvocationList();            Console.WriteLine("del1的委托调用列表中包含{0}个方法", ds.GetLength(0));            //开始执行委托            del1(5); //先调用obj1.Func1(),再调用obj1.Func2()            MyDelegate del2 = new MyDelegate(obj.Func1);            del2 += new MyDelegate(obj.Func2);            //组合委托            Delegate mul = del1 + del2;            ds = mul.GetInvocationList();            Console.WriteLine("mul的委托调用列表中包含{0}个方法", ds.GetLength(0));            int ret = (mul as MyDelegate)(10); //获取委托调用列表最后一个方法的返回值            Console.WriteLine("ret = {0}", ret);            Console.ReadKey();        }    }}


2、委托和匿名方法

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace UseAnonymousMethod{    //定义一个委托    public delegate int AddDelegate(int i, int j);    class Program    {        //委托可以将方法当做参数传递.        //使用委托类型的参数        public static void invokeDelegate(AddDelegate del, int i, int j)        {                        Console.WriteLine(del(i, j));        }        static void Main(string[] args)        {            //利用C#匿名方法特性直接给委托变量赋值            AddDelegate del = delegate(int i, int j)            {                return i + j;            };            //通过委托变量调用匿名方法            Console.WriteLine(del(100, 200));            //直接将匿名方法作为函数参数            invokeDelegate(delegate(int i, int j)            {                return i + j;            }, 100, 200);            Console.ReadKey();        }    }}

3、泛型委托

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace UseGenericDelegate{    //定义泛型委托    public delegate T MyGenericDelegate<T>(T obj);    class Program    {        static void Main(string[] args)        {            //使用匿名方法给泛型委托变量赋值            MyGenericDelegate<int> del = delegate(int vlaue)            {                return vlaue * 2;            };            //调用泛型委托变量引用的匿名方法            Console.WriteLine(del(100));            Console.ReadKey();        }    }}

4、使用TimerCallBack委托实现定时回调

using System;using System.Collections.Generic;using System.Text;using System.Threading;namespace UseTimerCallback{    //用于向回调函数提供参数信息    class TaskInfo    {        public int count = 0;    }    class Program    {        static void Main(string[] args)        {              System.Console.WriteLine("敲任意键结束……");              TaskInfo ti=new TaskInfo() ;              //创建Timer对象,将一个回调函数传给它,每隔一秒调用一次              Timer tm = new Timer(ShowTime, ti, 0, 1000);                          System.Console.ReadKey();              tm.Dispose();        }        //被回调的函数        static void ShowTime(Object ti)        {            TaskInfo obj = ti as TaskInfo;            obj.count++;            System.Console.WriteLine("({0}){1}", obj.count, DateTime.Now);        }    }}
委托可用于引用一个或多个方法,因此,多用于需要动态调用不同方法的场合