C # anonymous method

Source: Internet
Author: User

1. Introduction to anonymous methods
The anonymous method allows us to write the method in an "inline" way.CodeThe code is directly associated with the delegated instance, which makes the task of delegated instantiation more intuitive and convenient.

2. Questions about the anonymous method:

2.1 parameter list
The anonymous method can be followed by a parameter list (you can leave it unspecified) by the delegate keyword. The following code blocks can access these parameters:
Addbutton. Click + = delegate (Object sender, eventargs e) {MessageBox. Show (button) sender). Text );};

Note the difference between "No parameter list specified" and "parameter list blank"

Addbutton. Click + =   Delegate {...} // Correct!
Addbutton. Click + =   Delegate (){...} // Error!

2.2 Return Value
If the return value type of the delegate type is void, no value can be returned in the anonymous method. If the return value type of the delegate type is not void, the value returned by the anonymous method must be compatible with the return value of the delegate type:

Delegate   Int Mydelegate ();
Mydelegate d =   Delegate {
......
Return   100 ;
};

Delegate VoidMydelegate ();
Mydelegate d= Delegate{
......
Return;
};

2.3 external variables
Some local variables and parameters may be used by anonymous methods. They are called "external variables of anonymous methods ". The lifetime of the external variable is extended due to the capture benefit of the anonymous method-until the delegated instance is not referenced.

Static   Void Foo ( Double Factor)
{
Function f = Delegate ( Int X)
{
Factor + = 0 . 2 ; // Factor is an external variable.
Return X * Factor;
};
Invoke (f ); // The survival time of factor is prolonged.
}

3. Delegation type inference
C #2.0 allows us to omit the delegate type when instantiating the delegate, and directly use the method name. The C # compiler will make reasonable inferences.
• C #1.0 practices:

Addbutton. Click + =   New Eventhandler (addclick );
Apply (, New Function (math. Sin ));

• C #2.0 practices:

Addbutton. Click + = Addclick;
Apply (A, math. Sin );

4. Anonymous method Mechanism
In C #2.0, the anonymous method only simplifies the task of delegation instantiation through the additional processing of the compiler. It has no fundamental difference with C #1.0 code.

4.1 anonymous methods in static methods

Public   Delegate   Void D ();
Static   Void F ()
{
D =   Delegate {Console. writeline ( " Test " );}
}

The above code is converted:

Static   Void F ()
{
D =   New D ( < F > B _ 0 );
}

Static Void <F>B _ 0 ()
{
//The function name and class name may be different.
Console. writeline ("Test");
}

4.2 anonymous methods in the instance method

Class Test
{
Int X;
Void F ()
{
D =   Delegate {Console. writeline ( This . X );};
}
}

The above code is converted:

Void F ()
{
D =   New D ( < F > B _ 0 );
}
Void   < F > B _ 0 ()
{
Console. writeline ( This . X );
}

4.3 external variables in the anonymous method

Void F ()
{
Int Y =   123 ;
D =   Delegate {Console. writeline (y );};
}

Auto: http://blog.csdn.net/happyhippy/archive/2007/03/20/1534812.aspx

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.