您当前的位置: 首页 >  c#

刘一哥GIS

暂无认证

  • 5浏览

    0关注

    934博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

C#委托、事件学习之(一)——委托事件描述

刘一哥GIS 发布时间:2015-09-14 11:19:27 ,浏览量:5

1、委托是一个类,定义了方法的类型,使得该方法可以作为其他方法的参数。避免了if...else的大量使用。

using System;

namespace ConsoleApplication1
{
    public class GreetPeople
    {
        public delegate void GreetEventHander(object sender, GreetEventArgs e);
        public event GreetEventHander Greet;
        public class GreetEventArgs : EventArgs
        {
            public readonly string name;
            public GreetEventArgs(string Tem)
            {
                this.name = Tem;
            }
        }
        protected virtual void GreetWay(GreetEventArgs e)
        {
            if (Greet != null)
                Greet(this, e);//调用注册方法
        }
        public void Greeting(string name)
        {
            GreetEventArgs e = new GreetEventArgs(name);
            GreetWay(e);
        }
    }
    public class GreetWays
    {
        public void English(object sender, GreetPeople.GreetEventArgs e)
        {
   
关注
打赏
1665586602
查看更多评论
立即登录/注册

微信扫码登录

0.0903s