I. Introduction
All methods use a subset of elements from the same set. In C #2.0, the optional element set will continue to grow. Historically, apart from the C ++ inline method, a method requires a name, a return type, and a method body. Alternatively, you can use an access modifier and a parameter list. In C #2.0, the method name has changed from required to optional.
C #2.0 (usually. NET) introduces the anonymous method. An anonymous method can be used in any case where the proxy is used and the proxy is defined as inline. It does not require a method name, but has optional parameters and a method body.
To use the anonymous method, You Need To Know What proxy is. Therefore, before we discuss in detail when to use the anonymous method and the limitations of the anonymous method, Let us briefly review the proxy.
Ii. Proxy Review
The anonymous method is a compression method for declarations and use of proxies (if you have any questions about the proxy, please read it; otherwise, you can skip the following section ). Proxy, as a pointer to the function signature, already exists in the language before. NET. Remember, everything in a computer is actually bit and byte. By introducing the function pointer technology, it is possible to dynamically assign some unknown functions in the future to the pointer, and thus the event is born.
The basic method of using a function pointer is to assign the address of a function to a single pointer. To call this function through a pointer, the programmer must check it to determine whether the pointer is null and then indirectly call this function through this pointer. In short, to use a pointer, you must perform a null check, and now "A pointer corresponds to a function" as a restriction, it is time to end.
Looking back, the proxy will become the next evolutionary replacement of the original function pointer. A proxy is a class that encapsulates the pointer. Implicitly, the proxy in. NET is a multicast proxy. As a multicast proxy, it only means that there is no "one function corresponds to one pointer" restriction, because the multicast proxy class contains a pointer list. Containing an internal list means that the address of more than one function can be assigned to a single proxy. When the proxy-you can think of as "Event"-is triggered or called, all the internal list functions are called.
Note that in C #, we call the proxy in the same way as we used to call the method and call all the value assignment functions. However, we can still perform the null check. In Visual Basic. NET, the null check is implicitly included in the activity of the activation event.
In C #, the function address is inserted to a list by using an overloaded + = Operator and deleted by an overloaded-= Operator. C # You can also manually define how to add or delete a block. adding or deleting a block is similar to the get and set attributes for a proxy.
In C #1.0 and C #1.1, we typically assign the proxy instance to the event attribute. For example, in WinForms, a Button control exposes a Click event. The Click proxy type is EventHandler. EventHandler is a method that takes the object and EventArgs as parameters. Therefore, we can use any method that matches the signature of the proxy EventHandler to initialize an EventHandler object and assign the proxy to Click. The code looks like this:
Private void Form1_Load (object sender, EventArgs e)
{Button1.Click + = new EventHandler (OnClick );}
Private void OnClick (object sender, EventArgs e)
{Debug. WriteLine ("button1 clicked ");}
Because the Form Designer of WinForms and the WebForms page designer automatically add proxy bindings, we may not need to manually bind a proxy to create a lot of code.
3. The anonymous method is an inline proxy.
Usually, when we use a proxy, we always have a method. The signature of this method matches the proxy signature rules and can be used to initialize a proxy instance. The anonymous method is used to compress the initialization of methods and proxies to a single location.
Using the example in the previous section, we can see how the instantiation of the new EventHandler proxy is different from the OnClick method used to initialize the proxy. This part of the code can be compressed into an anonymous method:
Private void Form1_Load (object sender, EventArgs e ){
Button1.Click + = delegate
{
Debug. WriteLine ("button1 clicked ");
};
}
To create this anonymous method, note that we have deleted the OnClick method header and replaced the EventHandler proxy constructor with The OnClick method body word delegate. The resulting behavior is the same. If we want to use event parameters, we usually associate them with proxies. We can add an optional parameter list after the word delegate:
Private void Form1_Load (object sender, EventArgs e ){
Button1.Click + = delegate (object s, EventArgs ev)
{Debug. WriteLine ("object is" + s. ToString ());};
}
If you define proxy parameters, they must match the parameters defined by the proxy type. For example, the Click type is EventHandler. Therefore, if parameters exist, they must match the EventHandler parameter object and EventArgs.
The anonymous method can be used wherever a proxy is needed. The ref and out parameters can be used for anonymous methods, but the reference ref or out parameters in the global scope cannot be used. The anonymous method cannot use unsafe encoding, and the anonymous method cannot use statements such as goto, break, or continue in the way that the branch action jumps out of the code block of the anonymous method.
Iv. Market Survey results
Is the anonymous method good? Market research proves that the anonymous methods are really good because they can reduce the code overhead caused by instantiating the proxy and reducing the separation method. Market research also proves that the anonymous method enhances availability and maintainability. I think a well-named method can also achieve this. Is the following code easy to maintain?
Private void Form1_Load (object sender, EventArgs e)
{
BindClick (delegate {Debug. WriteLine ("button1 click ");});
}
Private void BindClick (EventHandler handler)
{
Button1.Click + = handler;
}
In this example, we pass a proxy to a method by passing the proxy as an anonymous method. It is a headache to keep the sequence and number of parentheses, semicolons, and square brackets.
If you reference the classic example, the anonymous method only reduces the overhead of creating a proxy and a method by removing threads (they use proxies. This is true, but the thread is not often used and it is very difficult to use it correctly. I was wondering how cautious it would be to make the code more private, not more public.
In terms of language, I like methods. But as a thing in actual development, the anonymous method may only prove that Microsoft's inventor is a little clever.
V. Summary
An anonymous method can prove that there are methods without names-they can be defined and used in any place where proxy can be used. The agent is the package of the event processor. The practical and universal value of the anonymous method remains to be proved by further practices. I suspect that the anonymous method will not be more useful than the operator overload, and it will be used less and less; but the anonymous method is now. NET, so it is necessary to identify the code when reading it.