C# Delegate学习1

本文为C#入门经典例子Ch13Ex02的学习总结:

1、该例子利用定时器创建了定时事件

 pollTimer = new Timer(100); //100ms
 pollTimer.Elapsed += new ElapsedEventHandler(CheckForMessage);
Line1:定义个一个延时100ms触发一次的定时器

Line2:在定时器定时事件添加了一个CheckForMessage事件,其中CheckForMessage为函数指针。

2、利用在CheckForMessage函数中的MessageArrived完成了委托函数的声明

private void CheckForMessage(object source, EventArgs e)
        {
            Console.WriteLine("Checking for new message.");
            if ((random.Next(9) == 0) && (MessageArrived != null))
            {
                MessageArrived(this, new MessageArrivedEventArgs("Hello Mum!"));
            }
        }
3、在程序开始时利用delegate关键字创建了一个两输入的MessageHandler的方法类

public delegate void MessageHandler(Connection source,MessageArrivedEventArgs e );
4、在Connection类中定义了一个MessageArrived方法对象

 public event MessageHandler MessageArrived; //定义了一个MessageArrived事件
PS 在类中使用委托类定义方法对象需在定义时使用event关键字

5、最后在函数主题中完成声明和实体的连接

myConnection1.MessageArrived += new MessageHandler(myDisplay.DisplayMessage);
对于通过事件定义的方法对象只能通过+=和-=对方法对象进行链接和解除链接

DisplayMessage实现代码如下所示:
public class Display
    {
        public void DisplayMessage(Connection source, MessageArrivedEventArgs e)
        {
            Console.WriteLine("Message arrived from; {0}", source.Name);
            Console.WriteLine("Message Text {0}", e.Message);
        }
    }
6、说说参数传递,由于最终链接的DisplayMessage有两个参量,所以在委托类建立和事件的定义上,都需要和最终的实现实体保持一致。

public delegate void MessageHandler(Connection source,MessageArrivedEventArgs e );
public event MessageHandler MessageArrived; //定义了一个MessageArrived事件
上面MessageHandler就相当于定义X(Connection source, MessageArrivedEventArgs e)的事件

7、贴上Connection类实现代码和主程序代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
namespace ConsoleApplication1
{
    public delegate void MessageHandler(Connection source,MessageArrivedEventArgs e );
    public class MessageArrivedEventArgs : EventArgs
        {
            private string message;
            public string Message
            {
                get
                {
                    return message;
                }
            }
            public MessageArrivedEventArgs()
            {
                message = "No message sent.";
            }
            public MessageArrivedEventArgs(string newMessage)
            {
                message = newMessage;
            }
        }
        
    public class Connection
    {
        public event MessageHandler MessageArrived; //定义了一个MessageArrived事件
        private Timer pollTimer;
        private static Random random = new Random();
        private void CheckForMessage(object source, EventArgs e)
        {
            Console.WriteLine("Checking for new message.");
            if ((random.Next(9) == 0) && (MessageArrived != null))
            {
                MessageArrived(this, new MessageArrivedEventArgs("Hello Mum!"));
            }
        }
        public Connection()
        {
            pollTimer = new Timer(100); //100ms
            pollTimer.Elapsed += new ElapsedEventHandler(CheckForMessage);  
        }       
        private string name; 
        public string Name
        {
            set
            {
                name = value;
            }
            get
            {
                return name;
            }
    
        }

        public void Connect()
        {
            pollTimer.Start();
        }
        public void Disconnect()
        {
            pollTimer.Stop();
        }

    }
}
主程序代码
 static void Main(string[] args)
        {
            /*Connection myConnection = new Connection();
            Display myDisplay = new Display();
            myConnection.MessageArrived += new MessageHandler(myDisplay.DisplayMessage);
            myConnection.Connect();
            Console.ReadKey();
             * */
            Connection myConnection1 = new Connection();
            myConnection1.Name = "First connection";
            Connection myConnection2 = new Connection();
            myConnection2.Name = "Second connection";
            Display myDisplay = new Display();
            myConnection1.MessageArrived += new MessageHandler(myDisplay.DisplayMessage);
            myConnection2.MessageArrived += new MessageHandler(myDisplay.DisplayMessage);
            myConnection1.Connect();
            myConnection2.Connect();
            Console.ReadKey();
        }


你可能感兴趣的:(C#,c#,delegate,对象)