SoFunction
Updated on 2025-03-07

Delegation and event study notes in C#

1. Delegate

Delegate delegate is also a type. Delegate can be declared anywhere a class can be declared. It passes the method as a parameter of another method, so that different methods can be passed, different functions can be completed, and the program is very extensible.

For example:

Suppose there is a computer here, some people will use it to write programs, some people will use it to play games, some people will use it to watch movies, some people will use it to listen to music and play games, and some people will use it to listen to music and read documents, and QQ.

This computer can be abstracted into a computer-like computer, with a method DoWork in it, and everyone must do their own things through this method.

When we don't need to delegate, we can implement some fixed things, which are not flexible enough:

For example:

Copy the codeThe code is as follows:

class Program {
        static void Main(string[] args) {
            Computer computer = new Computer();
("Zhu Bajie", "Watching a movie!");
("Sha Monk", "Writing a program!");
("Sun Wukong", "playing a game!");
        }
    }

    public class Computer {
        public void DoWork(string name, string work) {
            ("{0}{1}", name, work);
        }
    }

The above code is very rigid and can only do one thing when calling DoWork.

So it's the turn of commissioning to appear.

The code is as follows:

Copy the codeThe code is as follows:

public delegate void WorkEventHandle(string name);

    class Program {
        static void Main(string[] args) {
            Computer computer1 = new Computer();
            WorkEventHandle work = Do1;
("Sun Wukong", work);

            ("");

            work = Do2;
("Bajie", Do2);
        }

        static void Do1(string name) {
("{0} on QQ!", name);
("{0} Listening to music!", name);
("{0} is playing the game!", name);
        }

        static void Do2(string name) {
("{0} Listening to music!", name);
("{0} is writing code!", name);
        }
    }

    public class Computer {
        public void DoWork(string name, WorkEventHandle Work) {
            Work(name);
        }
    }

Entrusted summary:

Advantages: Delegation can be passed as a parameter of the method. Whoever wants to call DoWork in Computer to complete his own affairs will implement the delegate binding method, so that different methods can be passed according to their needs, making the program very scalable.

Disadvantages: We can assign values ​​to the delegate at will, which destroys the encapsulation of the program.

2. Events

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

Copy the codeThe code is as follows:

public delegate void WorkEventHandle(string name);

    class Program {
        static void Main(string[] args) {
            Computer computer1 = new Computer();
            += new WorkEventHandle(Do1);

("Sun Wukong");

            ("");
            += new WorkEventHandle(Do2);

("Bajie");
        }


        static void Do1(string name) {
("{0} on QQ!", name);
("{0} Listening to music!", name);
("{0} is playing the game!", name);
        }

        static void Do2(string name) {
("{0} Listening to music!", name);
("{0} is writing code!", name);
        }
    }

    public class Computer {
        public event WorkEventHandle Work;
        public void DoWork(string name) {
            if (Work != null) {
                Work(name);
            }
        }
    }

Summarize:

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