C # Delegation's understanding and basic learning
Delegate, delegation, is a very important concept in C #, using delegates to enable programmers to encapsulate a method reference within a delegate object. You can then pass the delegate object to the code that invokes the referenced method without knowing at compile time which method will be called. Unlike function pointers in C or C + +, delegates are object-oriented and type-safe. In the most understandable words, you can think of a delegate as something to execute a method (function). When using a delegate, you can treat it as if it were a class. It is declared first, then instantiated. Just a little different, a class is called an object or an instance after it is instantiated, but the delegate is still called a delegate after it is instantiated.
Like what:
delegate int getcalculatedvaluedelegate (int x, int y);
In the definition above, we define a delegate that represents a class of functions with the first argument being an integer x, the second being the integer y, and the return value of the function as an integer. Here, for the sake of convenience, we refer to this class of functions as functions with the same signature (signature) (Note: This signature is not a concept in a digital signature, but simply means that such a function has the same parameter list and return value).
Since a delegate is a type, it can be used to define parameters, variables, and return values. A variable defined by a delegate is used to hold a function entity with the same signature. Note that C # differs from C + + in that function pointers in C + + can only hold global or static functions, whereas delegate entities in C # can refer to any function.
Now 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 be able to make it, if the solution is really needed (VS), at the end of the article there is the author's contact information. Here also can basically understand the meaning of "delegate", originally need to call _2 function, but through _1 to call, that is, _2 operation by the _1 of the parameters of the entrusted service. It was to be Xiao Ming to the teacher's office to take the chalk box, because Xiao Ming and Xiaowen is a good friend, so Xiao Ming will be small text for him to take, so small Wencheng Xiao Ming's agent, Xiao Ming entrusted Xiaowen to get chalk box. Forget a bit, the delegate can also have a return value and parameters, this function is similar, but with the function has a certain difference, entrusted to the service of others.
Delegates are very common in C # as parameters. For example, to create a thread, a ThreadStart or Parameterizedthreadstart delegate is required as a parameter, and the function that the parameter refers to is used as the thread execution body when the threads execute. Another example of the:list<t> type of the Find method is a delegate, which leaves the developer with the question "How to find" or "how to find". Developers only need to define a function with a parameter of T, return a Boolean, implement the function body, and pass the function as a parameter to the Find method, and the element in the collection can be found.
A delegate is typically used as a return value in situations where the use of different delegates depends on different circumstances. This is a bit like Factory mode, but the delegate is used as a return value or is not used as a parameter so frequently.
Detailed in-depth explanations will be explained in the following articles.
Author's contact information:[email protected]
C # Delegation's understanding and basic learning