C # development history of delegation:. Net 1.x delegation =>. Net 2.x anonymous method =>. Net 3.0/3.5 lambda expressions

Source: Internet
Author: User
/* C # development history of delegation :. net 1.x delegate =>. net 2.x anonymous method =>. net 3.0/3.5 Lambda expression MS-help: // Ms. msdnqtr. v90.chs/dv_csref/html/6ce3f04d-0c71-4728-9127-634c7e9a8365.htm in C #1.0. Code Explicitly 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 later Program Lambda expressions should be used. The following example demonstrates the development of the delegate creation process from C #1.0 to C #3.0: */namespace microshaoft {using system; class test {// C #1.0 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 with // Inline code, called an "anonymous method. "This // method takes a string as an input parameter. testdelegate testdelb = delegate (string S // parameter) // anonymous function {console. writeline (s) ;};// C #3.0. A delegate can be 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) // parameter => // anonymous function {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. */}
Related Article

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.