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