C#: Understanding and using delegates

Understanding: A delegate defines a type of method, which is equivalent to a List of variables. Variables of the same type can be added to the List in sequence, and methods of the same type can also be added to the delegate in sequence.

analyze:

  • What is the type of method: Methods with the same return value type and parameters can be regarded as the same type.
  • How a delegate defines the type of a method:
    Insert image description here
    Getting started:
    In the above image, via delegate The keyword defines a delegate type named DelegateDemo.

1. Declare a delegate type variable through the delegate type DelegateDemo.
Insert image description here
2. Declare some methods according to the type of method defined by the delegate
Insert image description here
3. Specific use of the delegate
Insert image description here
Summary: The above This is my understanding of delegation and how to use delegation. Delegation and generic delegation in C# are not explained here. If you want to continue to understand, you can check out other information.

Attached: complete picture
Insert image description here

Guess you like

Origin blog.csdn.net/u011229164/article/details/106817113