How to use event and when to use

 

 

 

 

First continue the previous article:

A delegate is a type, and an event is an instance of the delegate type, plus a permission control of the event keyword

My understanding is like 

There is a class called Student, and the event is an instance of Student 

new 小美 = new student(){爸爸= "班主任", 颜值=颜值.超模级别}

   It's called Xiaomei, but Xiaomei is very special. She is the daughter of the head teacher, so there can only be some ordinary interactions. It is impossible to fall in love, and the authority is controlled .

The event keyword is like not being able to pick up

 

Then back to the topic: 

The function of the event (observer mode) is to complete fixed actions, separate variable actions, control them from the outside, and expand at will

This feature happens to be needed when building a framework, to separate variable actions through events to support expansion

Use of events in WinFrom:

 

Why does event need permission control?

Just to avoid external changes

First look at  the delegate of EventHandler 

 

In WinFrom, you can see this delegate very frequently, all of which are instantiated as events to trigger clicks or other events

Combined with the experience of Javascript, this object and EventArgs are similar to the event source object, and then take a look at the Eventargs class.

It also seems to be empty. Basically , after inheriting and inheriting, you can specify the event information to be returned. There will be examples later.

Standard event usage examples:

announcer:

      public class IphoneX
   {
        public string Type { get; set; }

        private decimal _Price;
        public decimal Price {
            get { return _Price; }
            set
            {
                if (value < this._Price)
                {
                    this.DiscountHandler?.Invoke(this, new XEventArgs() {
                        OldPrice = this.Price,
                        NewPrice = value
                    });

                }
                _Price = value;
            }
        }

        public event EventHandler DiscountHandler;
 
   }
    public class XEventArgs : EventArgs
    {
        public decimal NewPrice { get; set; }
        public decimal OldPrice { get; set; }
    }

subscriber:

public class Subscriber
   {
        public string Name { get; set; }

        public void SubscribMsg(object goods, EventArgs args)
        {
            var realArgs = (XEventArgs)args;
            var realGoods = (IphoneX)goods;
            Console.WriteLine($" {this.Name}!, {realGoods.Type} IphoneX 降价了  从 {realArgs.OldPrice }降到了{realArgs.NewPrice}!");
        }
   }

Binding relationship:

 static void Main(string[] args)
        {
            var iphoneX = new IphoneX()
            {
                Type = "国内版",
                Price = 6000
            };
         
            iphoneX.DiscountHandler += new Subscriber() { Name = "小美" }.SubscribMsg;

            iphoneX.Price = 5000;
            iphoneX.Price = 4000;
            iphoneX.Price = 3000;
            iphoneX.Price = 6000;
            Console.Read();
        }

result:

 

 

Guess you like

Origin blog.csdn.net/qq_36445227/article/details/90693891