码迷,mamicode.com
首页 > Windows程序 > 详细

C#学习之-----再论委托

时间:2015-06-12 20:36:13      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:

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

namespace ConsoleApplication7
{
        public delegate void EH(string strText);               //第一步:    定义新委托类型       (类)

        class EventSourse
         {
                public event EH Textout;                          //声明新委托变量         (实例变量)
                public void TriggerEvent()
              {
                   if (null != Textout)
                       Textout("Event Triggered");    //第三步:调用委托
              }
          }


        class TestApp
        {
               static void Main(string[] args)
               {
                EventSourse evsrc = new EventSourse(); //实例化EventSource类

                evsrc.Textout += new EH(catchEvent);//第二步:实例化evsrc.Textout----将catchEvent静态方法委托到ecsrc.Textout(实例变量)上
                evsrc.TriggerEvent();          ////验证委托//调用委托代表的方法  catch

                evsrc.Textout -= new EH(catchEvent);
                evsrc.TriggerEvent();

                TestApp theApp = new TestApp();
                evsrc.Textout += new EH(theApp.instancecatch);
                evsrc.TriggerEvent();
                Console.Read();
               }
               public static void catchEvent(string strText)
              {
                 Console.WriteLine(strText);            //通过Textout(strText),其中strText="Event Triggered"调用、catchEvent,所以输出  Event Triggered
              }
               public void instancecatch(string strText)
                {
                Console.WriteLine("instance" + strText);          
                 }

          }
}

 

技术分享

C#学习之-----再论委托

标签:

原文地址:http://www.cnblogs.com/try-nocatch/p/4572373.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!