C delegate event study notes

  • 2020-12-18 01:54:11
  • OfStack

1. The entrusted delegate

Delegate delegate is also a type that can declare a delegate anywhere a class can be declared. It passes a method as a parameter of another method, so that different methods can be passed to accomplish different functions and the program has good extensibility.

For example:

So let's say I have a computer here, and somebody's going to use it to write programs, somebody's going to use it to play games, somebody's going to use it to watch movies, somebody's going to use it to play games while listening to music, somebody's going to use it to read documents while listening to music. QQ.

This computer can be abstracted into a class called Computer, which has a method called DoWork that everyone has to do their thing with.

When we don't use the delegate, we can achieve 1 fixed thing, which is not flexible enough:

Such as:


class Program {
        static void Main(string[] args) {
            Computer computer = new Computer();
            computer.DoWork(" The pig 8 quit ", " Watching a movie! ");
            computer.DoWork(" The sand monk ", " Writing a program! ");
            computer.DoWork(" The Monkey King ", " Playing a game! ");
        }
    }     public class Computer {
        public void DoWork(string name, string work) {
            Console.WriteLine("{0}{1}", name, work);
        }
    }

The code above, which is pretty rigid, can only do one thing when calling DoWork.

So now it's the delegate's turn.

The code is as follows:


public delegate void WorkEventHandle(string name);     class Program {
        static void Main(string[] args) {
            Computer computer1 = new Computer();
            WorkEventHandle work = Do1;
            computer1.DoWork(" The Monkey King ", work);             Console.WriteLine("");             work = Do2;
            computer1.DoWork("8 quit ", Do2);
        }         static void Do1(string name) {
            Console.WriteLine("{0} In the QQ ! ", name);
            Console.WriteLine("{0} Listening to music! ", name);
            Console.WriteLine("{0} Playing a game! ", name);
        }         static void Do2(string name) {
            Console.WriteLine("{0} Listening to music! ", name);
            Console.WriteLine("{0} Writing code! ", name);
        }
    }     public class Computer {
        public void DoWork(string name, WorkEventHandle Work) {
            Work(name);
        }
    }

Summary of delegation:

Advantage: The delegate can be passed as a parameter to the method. Those who want to call DoWork in Computer to do their own thing, the implementation of the delegate binding method, so you can according to their own needs to pass different methods, so that the program has a good extensibility.

Cons: We can assign arbitrary values to the delegate, thus breaking the encapsulation of the program.

2. The event

In order to compensate for the delegate defect, we can only perform "+=" and "-=" operations on the event, and cannot assign (=) operations on the event.
The above example is implemented with events as follows:


public delegate void WorkEventHandle(string name);     class Program {
        static void Main(string[] args) {
            Computer computer1 = new Computer();
            computer1.Work += new WorkEventHandle(Do1);             computer1.DoWork(" The Monkey King ");             Console.WriteLine("");
            computer1.Work += new WorkEventHandle(Do2);             computer1.DoWork("8 quit ");
        }
        static void Do1(string name) {
            Console.WriteLine("{0} In the QQ ! ", name);
            Console.WriteLine("{0} Listening to music! ", name);
            Console.WriteLine("{0} Playing a game! ", name);
        }         static void Do2(string name) {
            Console.WriteLine("{0} Listening to music! ", name);
            Console.WriteLine("{0} Writing code! ", name);
        }
    }     public class Computer {
        public event WorkEventHandle Work;
        public void DoWork(string name) {
            if (Work != null) {
                Work(name);
            }
        }
    }

Conclusion:

Delegates can perform either "+=" and "-=" operations or assignment (=) operations. Delegates are not encapsulated
Events can only be "+=" and "-=" operations, and events are encapsulated.


Related articles: