Anonymous method and Lambda expression

Source: Internet
Author: User

For the purpose of mvvm learning, review the anonymous method and lambda expressions. Because there are few examples used previously, they are not very skilled. Baidu-related knowledge, I wrote this demo to demonstrate how to use it in a simple way.

Here, the emphasis is on lambda expressions in LINQ.

 
VaRFileslookup = files. tolookup (F => F. substring (F. lastindexof ('.'). Toupper ());

There are some differences. Use them in two different ways.
Debuglzq has written all the words to be written in the demo comments. There are many similar things on the Internet, so there is no ink here, as shown below:

 Using  System;  Namespace  Anonymous functions and lambda expressions {  Class  Program {  ///   <Summary>         ///  Func and action classes are special types that allow you to use a delegate without specifying a custom delegate type. They can be used throughout the. NET Framework.  ///           ///  Lambda expressions have widely replaced anonymous methods because they are introduced from C #3.0. Lambda expressions have many of the same functions as anonymous methods, but they are easier to use.  ///   </Summary>          ///   <Param name = "ARGs"> </param>          Static   Void Main ( String  [] ARGs ){ //  Action delegate encapsulate methods that do not return results. In other words, this delegate can only be used for the methods defined with the void keyword. Action < Int , Int > FUNA = Delegate ( Int X, Int Y) {console. writeline (x + y );}; //  Anonymous Method  Action < Int , Int > Funb = ( Int X, Int Y) => { Int Z = x + y; console. writeline (z );}; //  Lambda expressions  //  Output FUNA ( 3 , 4  ); Funb (  3 , 4  ); Console. readkey ();  // In addition to returning results, the system. func delegate is identical to the action delegate. The simplest func implementation has no parameters. Func < Int , Int , Int > Fun1 = Delegate ( Int X, Int Y ){ Return X * Y ;}; //  Anonymous Method  Func < Int , Int , Int > Fun2 = ( Int X, Int Y) => { Return X * Y ;}; //  Lambda expressions  //  Output Console. writeline (fun1 ( 3 , 4  ); Console. writeline (fun2 (  3 , 4 ); Console. readkey ();}}} 

A picture is better than a thousand words,ProgramRun the following command:

 

 

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.