zoukankan      html  css  js  c++  java
  • Explaining Delegates in C#

    I was thinking that the previous post on Events and Delegates was quite self-explanatory. A couple of days ago, I received an email which said that may be it is not that good as to explain what I want to achieve in life through that event. So, I thought why not write another post on Delegates and Events to make things clearer from a practical (or may be not so practical, "BUT" just another simple and practical example. There is no harm in trying, after all...)

    Here is the requirement...

    You have a class with a special number. You have another (or may be a lot of other) class which is using it. The requirement now is that, whenever the special number is changed, all the other guys should be notified about what this number was and what it has become!

    In this case,

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace EventAndDelegateDemo
    {
        //There are 3 rules which are MANDATORY when you are planning to create events
        //Rule 1> The delegate must be defined with two arguments
        //Rule 2> These arguments always represent TWO objects… 
        //          The Publisher (object that raised the event)
        //          Event Information object
        //Rule 3> The 2nd object HAS to be derived from EventArgs
    
        //Step 1> Create a class that inherits from EventArgs (To comply with Rule 3)
        class NotifyChangeEventArgs : EventArgs
        {
            //constructor to initialize the SpecialNumberOld and SpecialNumberNew property
            public NotifyChangeEventArgs(int SpecialNumberOld, int SpecialNumberNew)
            {
                this.SpecialNumberNew = SpecialNumberNew;
                this.SpecialNumberOld = SpecialNumberOld;
            }
            public int SpecialNumberNew { get; set; }
            public int SpecialNumberOld { get; set; }
        };
    
        class SpecialNumberClass // Publisher
        {
            //Publisher declares a special delegate and event (Rule 1 & 2)
            public delegate void SpecialNumberHandler(object source, NotifyChangeEventArgs e);
            public event SpecialNumberHandler OnSpecialNumberUpdate;
    
            //Constructor to set the value of the _specialNumber directly.
            //Notice that we are not setting the SpecialNumber property!
            public SpecialNumberClass(int number)
            {
                _specialNumber = number;
            }
    
            //This property will fire an event called OnSpecialNumberUpdate whenever called
            //The effect is that is the special number is changed from outside, all the guys
            //would be notified. It is upto them to listen to it or not listen to it.
            private int _specialNumber;
            public int SpecialNumber
            {
                get { return _specialNumber; }
                //Put a breakpoint here on set and step into (F11)
                set
                {
                    if (OnSpecialNumberUpdate != null)
                    {
                        //This is the guy who would fire that notify event called OnSpecialNumberUpdate
                        //Basically, before you fire that event, you can set up that value for EventArgs
                        //In my case, I have set the value of e's old value and new value...
                        //to the _specialNumber and value (which would be the new value)
                        //Notice that we are just firing the events with appropriate EventArgs...
                        //We haven't thrown any output as such yet!!!!
                        NotifyChangeEventArgs e = new NotifyChangeEventArgs(_specialNumber, value);
                        Console.WriteLine("Raising Events to all the subscribers...
    ");
                        OnSpecialNumberUpdate(this, e);
                    }
                }
            }
        };
    
        class SpecialNumberUpdateListener // Subscriber
        {
            //Here we are just creating a new Object called objSN for my SpecialNumberClass
            SpecialNumberClass objSN;
    
            //In this method, I would go ahead and bind my event
            public SpecialNumberUpdateListener(SpecialNumberClass spClass)
            {
                Console.WriteLine("SpecialNumber listener > Subscribing to the event...
    ");
                this.objSN = spClass;
    
                //Wiring the event so that the event is fired
                spClass.OnSpecialNumberUpdate += new SpecialNumberClass.SpecialNumberHandler(OnSpecialNumberUpdate);
            }
    
            //This is the event that would be invoked whenever you change the value
            //Try putting a break point here and see how it gets hit when the number changes
            //Also notice how we use the Event args to grab the details and finally print it out
            void OnSpecialNumberUpdate(object source, NotifyChangeEventArgs e)
            {
                Console.WriteLine("The number has changed from {0} to {1} ", e.SpecialNumberOld, e.SpecialNumberNew);
            }
        }
    
        class EventDemo
        {
            public static void Main()
            {
                //Creating a new Special Number (Publisher) class with initial value of 20
                SpecialNumberClass snc = new SpecialNumberClass(30);
                //Creating a Subscriber/listener class
                SpecialNumberUpdateListener s = new SpecialNumberUpdateListener(snc);
                //Changing the value so that the event is triggered.
                //Put a breakpoint and step into the code (F11)
                snc.SpecialNumber = 40;
                Console.ReadLine();
            }
        }
    }

    转:http://www.dotnetscraps.com/dotnetscraps/post/Explaining-Delegates-in-C-Part-3-(Events-again).aspx

  • 相关阅读:
    fread()创建文件和file_exists()文件缓存问题
    docker 常用命令(一)
    python学习资源
    python 爬虫之 selenium API
    卡方检验用于特征选择
    三种常用数据标准化方法
    oracle查看编码以及修改编码
    JAVA 泛型
    java中Class对象详解和类名.class, class.forName(), getClass()区别
    oracle 数据导入导出
  • 原文地址:https://www.cnblogs.com/shuaixf/p/3319193.html
Copyright © 2011-2022 走看看