C # Foundation Strengthening---delegates, lamada expressions, and events (middle)

Source: Internet
Author: User

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)

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.