C # basic enhancement-delegate, Lamada expressions, and events (medium ),

Source: Internet
Author: User

C # basic enhancement-delegate, Lamada expressions, and events (medium ),

2. Lamada expression

C # There are two types of anonymous functions: the anonymous method and the Lamada expression. In C # versions earlier than 2.0, the only way to create a delegate is to use the naming method. The anonymous method is introduced in C #2.0. The anonymous method has no name. From C #3.0, you can use a new syntax to assign the implementation code to the Delegate-Lamada expression.

Before learning about Lamada expressions, let's take a look at the anonymous method. The syntax for defining a delegate using an anonymous method is no different from the previous one, but there are some differences in instantiation.

Delegate created using the naming method

1 class Program 2 {3 private delegate void DelSayHi (string name); // defines delegate 4 static void Main (string [] args) 5 {6 Test ("Zhang San ", sayHelloByChinese); 7 Test ("Zhang San", SayHelloByEnglish); 8} 9 static void Test (string name, DelSayHi del) 10 {11 del (name ); 12} 13 static void SayHelloByEnglish (string name) 14 {15 Console. writeLine ("Hello," + name); 16} 17 static void SayHelloByChinese (string name) 18 {19 Console. writeLine ("hello," + name); 20} 21}


Delegate created using an anonymous method

1 class Program 2 {3 private delegate void DelSayHi (string name); // defines delegate 4 static void Main (string [] args) 5 {6 Test ("Zhang San ", delegate (string name) 7 {8 Console. writeLine ("hello," + name); 9}); 10 Test ("Zhangsan", delegate (string name) 11 {12 Console. writeLine ("Hello," + name); 13}); 14} 15 static void Test (string name, DelSayHi del) 16 {17 del (name); 18} 19}

The advantage of the anonymous method is that it reduces code writing and does not need to define methods called only by delegation. When an anonymous method is used, the compiler still defines a method. This method has only one automatically specified name, which we do not need to know. When using the anonymous method, we should follow two rules: first, the jump Statement (break, goto, or continue) cannot be used in the anonymous method to jump to the external of the anonymous method. Otherwise, the jump statement outside the anonymous method cannot jump to the inside of the anonymous method. Second, the anonymous method cannot access Insecure code or the ref and out parameters used outside the anonymous method, but other variables defined externally can be used in the anonymous method.

 

Now let's look at the Lamada expression ....

To create a Lambda expression, specify the input parameter on the left side of the Lambda operator => (if any), and then enter the expression or statement block on the other side.

Example 1:

 1  class Program 2     { 3         delegate int del(int i); 4         static void Main(string[] args) 5         { 6             del myDelegate = x => x * x; 7             int j = myDelegate(5); //j = 25 8             Console.WriteLine(j.ToString()); 9         }10     }

Example 2:

Nam

Example 3:

 1  static void Main(string[] args) 2         { 3             string mid = ",middle part,"; 4  5             Func<string, string> lamada = (string param) =>   6             { 7                 param += mid; 8                 param += " end of string."; 9                 return param;10             };11             Console.WriteLine(lamada("Start of string "));12         }

For ease of use, strings of the name and param types in example 2 and 3 can also be omitted without writing. There is only one parameter. You only need to write the parameter name. If multiple parameters are used by the delegate, put the parameter name in brackets.
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 ."));

If the Lamada expression has only one statement, no curly brackets or return statements are required in the method block. In this case, the compiler adds an implicit return statement. For example, in Example 1, it is equivalent to del myDelegate = x =>{ return x * x ;}. if there are multiple statements in the implementation code of the Lamada expression, you must add curly braces and return statements.

 

 

It's a bit fun. Go to bed first, and leave the event to the next article .... Good night

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.