2.Lamada-expression
There are two types of anonymous functions in C #: Anonymous methods and Lamada expressions. In the C # version prior to 2.0, the only way to create a delegate is to use a named method. Anonymous methods are introduced in c#2.0, and anonymous methods are methods that do not have a name. Starting with c#3.0, you can use a new syntax to assign an implementation code to a delegate--lamada expression.
Before we get to the Lamada expression, let's look at the anonymous method first. The syntax for defining delegates with anonymous methods is no different from the previous ones, but there are some differences when instantiating.
delegate created using a named method
1 class Program2 {3 Private Delegate voidDelsayhi (stringname);//Defining Delegates4 Static voidMain (string[] args)5 {6Test ("Zhang San", Sayhellobychinese);7Test ("Zhang San", sayhellobyenglish);8 }9 Static voidTest (stringname, Delsayhi del)Ten { One del (name); A } - Static voidSayhellobyenglish (stringname) - { theConsole.WriteLine ("Hello,"+name); - } - Static voidSayhellobychinese (stringname) - { +Console.WriteLine ("Hello,"+name); - } +}
delegates created by using anonymous methods
1 class Program2 {3 Private Delegate voidDelsayhi (stringname);//Defining Delegates4 Static voidMain (string[] args)5 {6Test ("Zhang San",Delegate(stringname)7 {8Console.WriteLine ("Hello,"+name);9 });TenTest ("Zhangsan",Delegate(stringname) One { AConsole.WriteLine ("Hello,"+name); - }); - } the Static voidTest (stringname, Delsayhi del) - { - del (name); - } +}
The advantage of an anonymous method is that it reduces code writing and does not have to define methods that are only called by delegates. When using an anonymous method, the compiler still defines a method that has only one automatically specified name that we do not need to know. When using anonymous methods, we should follow two rules: First, the anonymous method cannot use the Jump statement (Break,goto or continue) to jump to the outside of the anonymous method, conversely, the anonymous method outside the jump statement cannot jump to the inside of the anonymous method. The second is that unsafe code cannot be accessed inside an anonymous method, and the ref and out parameters that are used outside the anonymous method cannot be accessed, but other externally defined variables can be used in anonymous methods.
Okay, let's get to the Lamada expression ....
to create a lambda expression, you specify an input parameter, if any, on the left side of the lambda operator, and then enter the expression or statement block on the other side.
Example 1:
1 class Program2 {3 Delegate intDelinti);4 Static voidMain (string[] args)5 {6Del mydelegate = x + x *x;7 intj = MyDelegate (5);//j =8 Console.WriteLine (j.tostring ());9 }Ten}
Example 2:
Nam
Example 3:
1 Static voidMain (string[] args)2 {3 stringMID =", middle part,";4 5func<string,string> Lamada = (stringparam) =6 {7param + =mid;8param + ="end of String.";9 returnparam;Ten }; OneConsole.WriteLine (Lamada ("Start of String")); A}
For simplicity, the type string for name and param in examples 2 and 3 can also be omitted from the write. There is only one parameter, just write the parameter name. If the delegate uses more than one parameter, the parameter name is enclosed in parentheses.
String mid = ", middle part,";
func<string,string, string> Lamada = (start,end) = =
{
Start + = Mid;
End =start+end;
return end;
};
Console.WriteLine (Lamada ("Start of String", "End of String."));
Also, if the Lamada expression has only one statement, no curly braces and return statements are required within the method block, and the compiler adds an implicit return statement. As in Example 1, it is equivalent to del mydelegate = x =>{return x * x;}; If you have more than one statement in the implementation code of the Lamada expression, you must add curly braces and return statements.
A little play, first sleep, the incident to leave the next article .... Good night
C # Foundation Strengthening---delegates, lamada expressions, and events (middle)