C#中的事件与委托是什么啊?搞不太懂,大家能给讲一下吗?或者举个例子.谢谢!

解决方案 »

  1.   

    事件的本质就是函数指针。
    你可先声明一个委托A,然后再声明一个由委托A来实现的事件B。然后你在你的类的函数里加入事件B(当你执行这个函数时就会触发事件B)。触发事件B后要执行的程序由你实例化委托A时指向。
    click是界面的一个事件,click触发后的执行的程序由委托EventHandler去指向。
    ex:
       this.btn1.click += new EventHandler(btn1_click);
       public void btn1_click()
      {... }
      

  2.   

    /*事件*/
        public delegate void TimeEventHandler(string s);//委托声明
        class MyTime
        {
            public event TimeEventHandler Timer;//声明事件
            public void OnTimer(string s)
            {
                if (null != Timer)
                {
                    Timer(s);//引发事件
                }
            }
        }    class ProcessTime
        {
            //事件处理
            public void GenerateTime(string s)
            {
                Console.WriteLine("Hello {0}! The time is {1} now", s, DateTime.Now);
            }
        }
        class TestTime
        {
            public static void Main()
            {
                ProcessTime p = new ProcessTime();
                MyTime t = new MyTime();
                t.Timer+=new TimeEventHandler(p.GenerateTime);//把事件与事件处理联系起来
                t.OnTimer("Peter");
                Console.Read();
            }
        }
        /*委托*/
        delegate void TimeDelegate(string s);//委托声明
        class MyTime
        {
            public static void HelloTime(string s)
            {
                Console.WriteLine("Hello {0}! The time is {1} now", s, DateTime.Now);
            }        public static void GoodbyeTime(string s)
            {
                Console.WriteLine("Goodbye {0}! The time is {1} now", s, DateTime.Now);
            }        public void SayTime(string s)
            {
                Console.WriteLine("{0}! The time is {1} now", s, DateTime.Now);
            }
        }    class TestDelegate
        {
            public static void Main()
            {
                //委托实例化,创建委托实例a,并封装静态方法HelloTime
                TimeDelegate a = new TimeDelegate(MyTime.HelloTime);
                Console.WriteLine("Invoking delegate a:");
                //委托调用,相当于调用方法MyTime.Hello("A")
                a("A");
                TimeDelegate b = new TimeDelegate(MyTime.GoodbyeTime);
                Console.WriteLine("Invoking delegate b:");
                b("B");
                //委托实例c封装了两个方法HelloTime和GoodbyeTime
                TimeDelegate c = a + b;
                Console.WriteLine("Invoking delegate c:");
                c("C");
                c -= a;
                Console.WriteLine("Invoking delegate c:");
                c("C");
                MyTime t = new MyTime();
                TimeDelegate d = new TimeDelegate(t.SayTime);
                Console.WriteLine("Invoking delegate d:");
                d("D");
                Console.Read();
            }
        }
      

  3.   

    我写了一个简单的委托例子
    using System;namespace consign
    {
    /// <summary>
    /// Class1 的摘要说明。
    /// </summary> //定义委托
    public delegate double text(int x,int y);
    class Class1
    {
    /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main(string[] args)
    {
    text t1 = new text(text1);
    Console.Write("输入两个值");
    int v1 = Int32.Parse(Console.ReadLine());
    int v2 = Int32.Parse(Console.ReadLine()); double res = t1(v1,v2);
    Console.WriteLine("结果 :"+res);
    Console.ReadLine(); }

    static double text1(int e,int r)
    {
    return e*r;
    } }
    }
    个人感觉就是一个方法不能确定他的参数等需要的时候就定义一个委托,然后让这个委托去指向一个方法。