C - event and commission

Event

Event is basically a user operation, such as button, click, mouse move, etc., or some emergence, such as system generated notifications. The application needs to respond to events when they occur. For example, interrupt. Events are used for interprocess communication.

Event owner: also known as event publisher. I think it is more convenient to understand with the owner. Obviously, whoever defines an event is the owner of the event. Specific to the code, that is, which class defines the event, then the instance of this class is the owner of the event.

Event subscriber: an object that can be understood as "staring" at a certain behavior of the event owner.

Event handling method: the method to be executed after the event is triggered. When the event subscriber "discovers" the event owner's event is triggered, the event subscriber needs to execute the method. With careful taste, it is not difficult to find that the event handling method is defined in the event subscriber, and the following code will be reflected.

 class Server
    {
        public delegate void StartDelegate();
        public event StartDelegate StartEvent;
        public void StartGame()
        {
            if (StartEvent!=null)
            {
                StartEvent();
            }
        }
    }
    class Client
    {
        private int id;
        public Client(int id)
        {
            this.id = id;
        }
        public void OnStart()
        {
            Console.WriteLine("{0}Client is starting",id);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Server s = new Server();
            Client c1 = new Client(1);
            Client c2 = new Client(2);
            Client c3 = new Client(3);
            Client c4 = new Client(4);
            s.StartEvent += c1.OnStart;
            s.StartEvent += c2.OnStart;
            s.StartEvent += c3.OnStart;
            s.StartEvent += c4.OnStart;
            s.StartGame();
            Console.ReadLine();
        }
    }

Entrust

A Delegate in C# is similar to a pointer to a function in C or C++. Delegate is a reference type variable that holds a reference to a method. References can be changed at run time.

Delegates are specifically used to implement events and callback methods. All delegates derive from the System.Delegate class.

using System;
using static Jerry.Cal;

namespace Jerry
{
    public static class Cal
    {
        //Define a delegate to calculate the sum of two numbers
        public delegate int AddDelegate(int a,int b);

        public static int AddFunction(int a, int b)
        {
            return a + b;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            AddDelegate del1 = new Cal.AddDelegate((a, b) => { return a + b; });
            AddDelegate del2 = ((a, b) => { return a + b; });
            AddDelegate del3 = AddFunction;

            int sum1 = del1(2, 3);
            int sum2 = del2.Invoke(4, 8);
            int sum3 = del3(8,9);
            Console.WriteLine(sum1);
            Console.WriteLine(sum2);
            Console.WriteLine(sum3);
        }
    }
}

Tags: Programming

Posted on Wed, 08 Jan 2020 06:24:17 -0800 by not_john