Anonymous functions (C # programming guide)

Source: Internet
Author: User

An anonymous function is an "inline" statement or expression that can be used wherever the delegate type is required.You can use an anonymous function to initialize the name delegate, or pass the name delegate (instead of the name delegate type) as the method parameter.

There are two types of anonymous functions, which are discussed in the following topics:

    • Lambda expressions (C # programming guide).

    • Anonymous method (C # programming guide)

      Description

      Lambda expressions can be bound to the expression tree or a delegate.

Development of delegation in C #

In C #1.0CodeExplicitly initialize the delegate to create the delegated instance. C #2.0 introduces the concept of anonymous methods, as a way to compile unnamed inline statement blocks that can be executed in a delegate call. C #3.0 introduces lambda expressions, which are similar to anonymous methods but more expressive and concise. These two functions are collectively referred to as "anonymous functions ". Typically, applications with. NET Framework Version 3.5 and laterProgramLambda expressions should be used.

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

C # copy
 Class Test { Delegate   Void Testdelegate ( String S ); Static   Void M ( String S) {console. writeline (s );} Static   Void Main ( String [] ARGs ){ // Original delegate syntax required          // Initialization with a named method. Testdelegate testdela = New Testdelegate (m ); // C #2.0: A delegate can be initialized         // Inline code, called an "anonymous method." This          // Method takes a string as an input parameter. Testdelegate testdelb = Delegate ( String S) {console. writeline (s );}; // C #3.0. A delegate can be initialized          // 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 .*/ 

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.