Recursive call in lambda expressions

Source: Internet
Author: User

Lambda expressions are anonymous methods. During the development period, we don't know what the method name is. So how can we call them recursively?

If you see meArticleIf you know who called your method, you should take a look at your mind and come up with ideas. That's right !! That is, you can directly use stackframe to obtain the method of the current execution, and then directly use invoke.

ExampleCode:

Class Test
{
/**/ ///   <Summary>
///  
///   </Summary>
///   <Param name = "V1"> </param>
///   <Param name = "V2"> </param>
///   <Param name = "num"> </param>
///   <Param name = "Total"> </param>
///   <Returns> </returns>
Public   Delegate   Long Addmethod ( Long V1, Long V2, Int Num, Int Cout );

/**/ ///   <Summary>
///  
///   </Summary>
///   <Param name = "addmethod"> </param>
///   <Param name = "cout"> </param>
Static   Void Print (addmethod method, Int Cout)
{
Console. writeline (method (0,1, 2, Cout ));
}

Static   Void Main ()
{
// Evaluate the number of similar data queues in 30th locations.
Print (V1, V2, num, cout) =>
{
Console. Write (V1 );
Console. Write ( " , " );

Num ++ ;
If (Num = Cout)
{< br> console. write (V2);
console. writeline ();
return V1 + V2;
}
Else
{
// Obtain the current method
Stackframe =   New Stackframe ( 0 );
// Recursive call
Return ( Long ) (Frame. getmethod (). Invoke (Num, New   Object [] {V2, (V1+V2), num, cout} ));
}
} , 30 );
Console. Read ();
}
}

The code is successfully run. The value of the successful code in the 30th position is: 514229.

At that time, there is another thing in. Net that can be easily referenced in the current call method, which is Methodbase The static method getcurrentmethod can directly obtain the reference of the currently executed method, so we can change the code of the two statements at stackframe to the following: // Recursive call
Return ( Long ) (Methodbase. getcurrentmethod (). Invoke (Num, New   Object [] {V2, (V1+V2), num, cout} ));


The results after execution are the same (orz is not the same)

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.