C # understanding and basic learning of delegation
Zookeeper
C # understanding and basic learning of delegation
Delegate and delegation are very important concepts in c #. Using delegation allows programmers to encapsulate method references in the Delegate object. The delegate object can then be passed to the code that can call the referenced method, without knowing which method will be called during compilation. Unlike function pointers in C or C ++, delegation is object-oriented and type-safe. In terms of ease of understanding, you can regard delegation as a thing used to execute methods (functions. When using a delegate, you can treat it like a class. That is, declare the object first, and then instantiate the object. The class is called an object or an instance after instantiation, but the delegate is still called a delegate after instantiation.
For example:
Delegate int GetCalculatedValueDelegate (int x, int y );
In the above definition, we define a delegate, which represents a class of functions. The first parameter of these functions is integer x, and the second parameter is Integer y, the return value of a function is an integer. Here, for the convenience of description, we call this type of function a function with the same signature (Note: This signature is not a concept in the digital signature, but only indicates that such functions have the same parameter list and return values ).
Since a delegate is a type, it can be used to define parameters, variables, and return values. Variables defined by the delegate are used to save function entities with the same signature. Note that, unlike C # And C ++, function pointers in C ++ can only save global or static functions, while delegate entities in C # can refer to any function.
Let's look at an example,
delegate string Get(); static string _1(Get get) { return get(); } static string _2() { return "123"; } private void button1_Click(object sender, EventArgs e) { label1.Text = _1(_2); }The above example is a form-based example, but most readers of the form should do it. If you really need a solution (in VS), there will be a contact information from the author at the end of the article. Here, we can basically understand the meaning of "delegation". The _ 2 function is called through _ 1, that is, the _ 2 operation is performed on behalf of the _ 1 parameter. James was supposed to go to the teacher's office to get a chalk box. Because james and Xiaowen are good friends, James asked Xiaowen to take it for him, so Xiaowen became Xiao Ming's agent, james commissioned Xiaowen to get the chalk box. Forget that the delegate can return values and parameters. This function is similar, but it is different from the function. The delegate serves others.
Delegation is a common parameter in C. For example, to create a thread, You need to delegate a ThreadStart or ParameterizedThreadStart as a parameter. During thread execution, the function referred to by this parameter is used as the thread execution body. Another example is List. Parameters of the Find method of the type are also a delegate, which leaves the problem of "how to Find" or "How to Find" to developers. Developers only need to define a function whose parameter is T and return a Boolean value, implement the function body, and pass the function as a parameter to the Find method to complete element search in the set.
The return value of a delegate is generally used in the case that different delegates are used according to different situations. This is a bit like the factory mode, but the delegate is often used as the return value without being used as a parameter.