Lambda expression anonymous function

Source: Internet
Author: User

An anonymous function is an "inline" statement or expression that can be used anywhere a delegate type is required.
You can use anonymous functions to initialize a named delegate, or to pass a named delegate (not a named delegate type) as a method parameter.

the development of delegation in C #

In C # 1.0, you create an instance of a delegate by explicitly initializing the delegate by using a method that is defined elsewhere in the code.C # 2.0 introduces the concept of an anonymous method as a way to write an unnamed inline statement block that can be executed in a delegate invocation.c# 3.0 introduces Lambda expressions, which are similar to the concept of anonymous methods, but are more expressive and concise.    These two functions are collectively referred to as "anonymous functions".    Typically, applications that target the. NET Framework version 3.5 and later should use LAMBDA expressions.   

The following example shows the development of the delegate creation process from C # 1.0 to C # 3.0:

classtest{Delegate voidTestdelegate (strings); Static voidMstrings) {Console.WriteLine (s); }    Static voidMain (string[] args) {        //Original delegate syntax required//initialization with a named method.Testdelegate Testdela =Newtestdelegate (M); //C # 2.0:a delegate can be initialized with//inline code, called an "anonymous method." this//method takes a string as an input parameter.Testdelegate Testdelb =Delegate(strings) {Console.WriteLine (s);}; //C # 3.0. A delegate can initialized with//a lambda expression. The lambda also takes a string//As an input parameter (x). The type of x is inferred by the compiler.Testdelegate TESTDELC = (x) = ={Console.WriteLine (x);}; //Invoke the delegates.Testdela ("Hello. My name is M and I write lines."); Testdelb ("That's nothing . I ' m anonymous and"); TESTDELC ("I ' m a famous author."); //Keep Console window open in debug mode.Console.WriteLine ("Press any key to exit.");    Console.readkey (); }}/*Output:hello.    My name is M and I write lines. That's nothing.    I ' m anonymous and I ' m a famous author. Press any key to exit. */

Lambda expression anonymous function

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.