C#委托与事件

定义委托语法:

访问修饰符delegate 返回值类型 委托名称(参数1,参数2,参数n …)

定义事件的语法:

public event 委托名 事件名;

 

控制台应用程序实例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Event
{
    class Program
    {
        static void Main(string[] args)
        {
            Employee e1 = new Employee("小白",988);
            Reporter re = new Reporter();

            //指明玩游戏这个事件触发后,由谁来处理
            e1.PlayGame += new DelegateClassHandle(re.Notify);

            e1.Game();
        }

    }

    public class Employee
    {
        /// <summary>
        /// DelegateClassHandle的事件
        /// </summary>
        public event DelegateClassHandle PlayGame;

        private string _name;

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
        private int _number;

        public int Number
        {
            get { return _number; }
            set { _number = value; }
        }

        public Employee(string n,int nu)
        {
            _name = n;
            _number = nu;
        }

        public void Game()
        {
            Console.WriteLine("我要玩游戏喽!");

            CustomEventnArgs e = new CustomEventnArgs();
            e.Name = _name;
            e.Number = _number;
            if (PlayGame != null)
            {
                PlayGame(this,e);
            }
        }
    }
    
    /// <summary>
    /// 委托
    /// </summary>
    /// <param name="sender">触发事件的源</param>
    /// <param name="e">同时携带的参数</param>
    public delegate void DelegateClassHandle(object sender,CustomEventnArgs e);

    /// <summary>
    /// 具体报告
    /// </summary>
    public class Reporter
    {
        public void Notify(object sender,CustomEventnArgs e)
        {
            Console.WriteLine("报告老板,{0}在{1}办公室玩游戏!",e.Name,((Employee)sender).Number);
            //或者
            //Console.WriteLine("报告老板,{0}在{1}玩游戏!", e.Name, e.Number);
        }
    }

    public class CustomEventnArgs : EventArgs
    {
        string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        int number;

        public int Number
        {
            get { return number; }
            set { number = value; }
        }

        public CustomEventnArgs()
        {

        }


    }
}
posted on 2013-11-09 17:45  神秘藏宝室  阅读(282)  评论(0编辑  收藏  举报

 >>>转载请注明出处<<<