C # 2.0 specification (anonymous method) (i)

Source: Internet
Author: User
Tags anonymous modifier

21 Anonymous method 21.1. Anonymous method expression

An anonymous method expression (anonymous-method-expression) defines an anonymous method (anonymous), which evaluates to a specific value that references the method.

L Primary-no-array-creation-expression (basic non-array creation expression:)

...

Anonymous-method-expression (anonymous method expression)

L Anonymous-method-expression:

Delegate anonymous-method-signature opt block (anonymous method expression: Delegate anonymous method signature optional blocks)

L Anonymous-method-signature:

(Anonymous-method-parameter-list opt) (anonymous method signature: Optional anonymous method argument list)

L Anonymous-method-parameter-list:

Anonymous-method-parameter

Anonymous-method-parameter-list, Anonymous-method-parameter (anonymous method argument list: Anonymous method parameter anonymous method parameter list)

L Anonymous-method-parameter:

Parameter-modifieropt type identifier (anonymous method parameter: parameter modifier optional type identifier)

An anonymous method expression is categorized as a value with a specific transformation rule (§21.3).

Anonymous method expressions define a new declaration space for parameters, local variables, and constants, and a new declaration space is defined for the label (§3.3).

21.2 Anonymous method signature

The optional anonymous method signature (anonymous-method-signature) defines the name and type of the formal parameter for the anonymous method. The parameter scope of an anonymous method is block. Matching the name of a local variable, local constant, or parameter whose scope contains an anonymous method expression (anonymous-method-expression) is a compile-time error for the name of an anonymous method parameter.

If an anonymous method expression has an anonymous method signature, then the compatible delegate type is limited to the collection of delegate types that have the same parameter types and modifiers of the same order (§21.3). If an anonymous method expression does not have an anonymous method signature, then the compatible delegate type is limited to the collection of delegate types that do not have an output parameter.

Note that anonymous method signatures cannot contain attributes or array of parameters. However, an anonymous method signature can be compatible with a delegate type that contains a parameter array in its argument list.

21.3 Anonymous Method conversions

Anonymous method expressions are categorized as a value of no type. An anonymous method expression can be used in a delegate creation expression (§21.3.1). All other legitimate uses of an anonymous method expression depend on the implicit conversion defined here.

An implicit conversion exists from an anonymous method expression that is compatible with any delegate. If D is a delegate type, and a is an anonymous method expression, D is compatible with a if the following conditions are true:

L First, the parameter type of D is compatible with a:

N If a does not contain anonymous method signatures, then D can have 0 or more parameters of any type, provided that D does not have any parameters with an output parameter modifier.

N If a has an anonymous method signature, then D must have the same number of arguments, each parameter of a must have the same type as the corresponding parameter of D, and the existence of the ref or out modifier for each parameter on a must match the corresponding parameter of D. Whether the last parameter of D is a parameter array and D has nothing to do with the compatibility of a.

L Second, the return type of D must be compatible with a, and for these rules, it does not take into account the case that a contains any other anonymous method blocks.

N if D uses the Void declaration return type, then any return statement contained in a should not specify an expression.

N if D uses type R to declare the return type, then any return statement contained in a must specify an expression that can be implicitly converted (§6.1) to R. Also, the end point of a block must be not reached.

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.