Suspected analysis of anonymous methods in C # 2.0

Source: Internet
Author: User
Tags anonymous require

First, Introduction

All methods use a subset of elements from the same collection. In C # 2.0, the set of optional elements will continue to grow. Historically-except for the C + + inline Method-Methods require a name, a return type, and a method body. Optionally, the method can use an access modifier and a list of parameters. In C # 2.0, the method name has been changed from required to optional.

C # 2.0 (typically on behalf of. NET) introduces anonymous methods. An anonymous method can be used when any proxy is used and is defined as inline, it does not require a method name and has optional arguments and a method body.

To use anonymous methods, you need to know what a proxy is. So before we discuss in detail when to use anonymous methods and the limitations of anonymous methods, let's briefly review the agent.

Second, the agent review

Anonymous methods are a compression method for declaring and using proxies (if you have questions about what the agent is, please continue reading; otherwise, you can skip this section below). Proxy, as a pointer to a function signature, that already exists in the language before the. NET language. Remember, everything is bit and byte in the computer. By introducing function pointer technology, it is possible to dynamically assign some future unknown functions to the pointer, and thus the event is born.

The basic method of using a function pointer is to assign a function's address to a single pointer. To invoke the function through a pointer, the programmer checks to see if the pointer is null, and then indirectly calls the function through the pointer. In short, to use pointers, you must perform a null check, and now that "one pointer corresponds to a function" as a limitation it's time to end it.

In retrospect, the agent becomes the next evolutionary replacement for the original function pointer. An agent is a class that encapsulates the pointer, implicitly,. NET agent is the multicast agent. Being a multicast proxy simply means there is no longer a "one function for one pointer" restriction, because the multicast proxy class contains a list of pointers. Including an internal list means that the address of more than one function can be assigned to a single proxy. When the agent-you can think of "event"-is fired or invoked, all the internal list functions are invoked.

Notice in C # that we invoke the proxy in the same way that we call the method and call all the assignment functions, but we can still do a null check. In Visual Basic.NET, a null check is implied in the activation event behavior.

In C #, the function address is inserted into a list by using an overloaded + = operator and is deleted through an overloaded-= operator. C # also supports the manual definition of adding and deleting blocks, as well as the addition and deletion of properties for proxies like get and set.

In C # 1.0 and C # 1.1, Typically, we assign a proxy instance to an event property. For example, in WinForms, a button control exposes a click event. The agent type of click Is EventHandler. EventHandler is a method that takes objects and EventArgs as parameters. Therefore, we can initialize a EventHandler object using any method that matches the signature of the proxy EventHandler and assign the agent to click. Here's what the code looks like:

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 WinForms form designer and the WebForms page designer automatically add proxy bindings, it is possible to create a lot of code without manually binding the broker.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.