C # 2.0 specification (anonymous method) (ii)

Source: Internet
Author: User
Tags microsoft c

21.7 Delegate instance Equality

The following rules apply the equality operator (§7.9.8) and object for an instance that is delegated by an anonymous method. The result that the Equals method produces.
When a delegate instance is generated by an anonymous method expression that has the same semantics as the same captured set of external variables, it can be said (but not required) that they are equal.
When a delegate instance is represented by an anonymous method expression that has different semantics, or has a different collection of external variables that are caught, they are never equal.

21.8 definite assignment

The explicit assignment state of an anonymous method parameter is the same as the named method. That is, the reference parameter and the value parameter are explicitly assigned initial values, and the output parameters are not assigned the initial value. Also, the output parameter must be definitely assigned (§5.1.6) before the anonymous method returns to normal.
When controlling a block that is converted to an anonymous method expression, the explicit assignment state to the external variable v is the same as the explicit assignment state of v before the anonymous method expression. That is, the explicit assignment of an external variable is inherited from the anonymous method expression context. Within an anonymous method program block, the explicit assignment will be interpreted as if it were within a normal program block (§5.3.3).
The explicit assignment state of the variable v after the anonymous method expression is the same as its explicit assignment state before the anonymous method expression.

For example

delegate bool Filter (int i); void F () {int max;//error, max not explicitly assigned Filter F = delegate (int n) {return n < max;} max = 5;dowork (f);}

A compile-time error is generated because Max is not explicitly assigned at the location of the anonymous method declaration. Example

delegate void D (), void F () {int n;d D = delegate {n = 1;}; D ();//error, n not explicitly assigned Console.WriteLine (n);}

A compile-time error is also generated because the assignment of n within the anonymous method has no effect on the explicit assignment state of the outer n of the anonymous method.

21.9 Method Group Conversions

Similar to implicit anonymous method conversions described in §21.3, there is an implicit conversion from a method group (§7.1) to a compatible delegate type.
For a given method group E and delegate type D, if a delegate of the form new D (e) is allowed to create an expression (§7.5.10.3 and §20.9.6), then there is an implicit conversion from E to D, and the result of the conversion is exactly equivalent to new D (e).
In the following example

Using system;using system.windows.forms;class Alertdialog{label message = new Label (); Button OKButton = New button (); Button CancelButton = New button (); ' Public Alertdialog () {Okbutton.click + = new EventHandler (Okclick); Cancelbutton.click + = new EventHandler (Cancelclick); ...} void Okclick (object sender, EventArgs e) {...} void Cancelclick (object sender, EventArgs e) {...}}

The constructor creates two delegate instances with new. Implicit method group conversions allow you to simplify them to

Public Alertdialog () {Okbutton.click + = Okclick;cancelbutton.click + = Cancelclick; ...}

For all other implicit and explicit conversions, the conversion operator can be used to explicitly perform a specific conversion. To do this, the example

Object obj = new EventHandler (Mydialog.okclick);

Can be replaced by the following look.

Object obj = (EventHandler) Mydialog.okclick;

Method combinations anonymous method expressions can affect overload resolution (overload resolution), but they do not participate in type inference. See §20.6.4 for more detailed information.

21.10 Implementation Examples

This section describes the possible implementations of anonymous methods in the form of standard C # artifacts. The implementation described here is based on the same principles used by the Microsoft C # compiler, but it is by no means mandatory or the only possible implementation.
The following sections of this section give some sample code that contains anonymous methods that have different characteristics. For each example, we will provide a corresponding transformation of the code that uses the unique standard C # artifacts. In these examples, the identifier D is assumed to represent the following delegate type.

public delegate void D ();

The simplest form of an anonymous method is the one that does not capture an external variable.

Class test{static void F () {d d = delegate {Console.WriteLine ("Test");};}}

This code can be converted to a delegate instance that references a static method generated by the compiler, and the code for the anonymous method is put into that static method. 、

Class test{static void F () {d d = new D (__METHOD1);} static void __method1 () {Console.WriteLine ("Test");}}

In the following example, an anonymous method references an instance member of this.

Class Test{int X;void F () {d d = delegate {Console.WriteLine (x);};}}

This can be converted to an instance method that is generated by the compiler that contains anonymous method code.

Class Test{int X;void F () {d d = new D (__METHOD1);} void __method1 () {Console.WriteLine (x);}}

In this example, the anonymous method captures a local variable.


Class Test{void F () {int y = 123;d D = delegate {Console.WriteLine (y);};}}

The lifetime of the local variable must now extend at least until the lifetime of the anonymous method delegate. This can be done by adding the local variable "lift (lifting)" to the field of the compiler-generated (compiler-generated) class. The instantiation of a local variable corresponds to the creation of an instance of a compiler-generated class, and access to the local variable corresponds to a field that accesses the compiler-generated class instance. Also, the anonymous method becomes an instance method of the compiler-generated class.

Class Test{void F () {__locals1 = new __locals1 (); __locals1.y = 123;d d = new D (__LOCALS1.__METHOD1);} class __locals1{public int y;public void __method1 () {Console.WriteLine (y);}}}

Finally, the following anonymous method captures this and two local variables with different lifetimes.

Class Test{int X;void F () {int y = 123;for (int i = 0; i <; i++) {int z = i * 2;d D = delegate {Console.WriteLine (x + y + z); };}}}

Here, the compiler will generate classes for each statement block, where local variables will be captured, and local variables in different blocks will have independent lifetimes.

An instance of __LOCALS2, a class generated by the compiler for an inner statement block, containing local variable z and a field referencing the __LOCALS1 instance. An instance of __LOCALS1, a class generated by the compiler for an outer statement block, containing the local variable y and a field referencing the this of the enclosing function member. With these data structures, you can reach all the captured local variables through an instance of __LOCALS2, and the code for the anonymous method can be implemented as an instance method of that class.

Class Test{void F () {__locals1 = new __locals1 (); __locals1.__this = THIS;__LOCALS1.Y = 123;for (int i = 0; i <; i++) {__locals2 = new __locals2 (); __locals2.__locals1 = __locals1;__locals2.z = i * 2;d d = new D (__LOCALS2.__METHOD1);}} Class __locals1{public Test __this;public int y;} Class __locals2{public __locals1 __locals1;public int z;public void __method1 () {Console.WriteLine (__locals1.__this.x + __locals1.y + z);}}}

(anonymous method finished)


The above is the C # 2.0 specification (anonymous method) (ii) content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    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.