Anonymous functions, delegation, and Lambda

Source: Internet
Author: User

A delegate is a type of reference method. Once a method is assigned to the Delegate, the delegate has the same behavior as the delegate. A call to a delegate method can have parameters and return values like any other method.

Delegation has the following features:

    • Delegation is similar to C ++ function pointers, but they are type-safe.

    • The delegate allows passing methods as parameters.

    • A delegate can be used to define a callback method.

    • The delegate can be linked together. For example, multiple methods can be called for an event.

    • The method does not have to match the delegate signature completely.

    • C #2.0 introduces the concept of anonymous methods, which allowCodeBlock is passed as a parameter to replace the separately defined method. C #3.0 introduces lambda expressions that allow you to write Inline code blocks more concisely. Both anonymous methods and lambda expressions (in some contexts) can be compiled as delegate types. These functions are collectively called anonymous functions.

The "anonymous method" is a method without a name. The anonymous method is usually used to pass a code block as a delegate parameter.

 

A Lambda expression is an anonymous function that can contain expressions and statements and can be used to create a delegate or expression directory tree.

All lambda expressions use the lambda operator =>, which reads "goes ". The left side of the lambda operator is the input parameter (if any), and the right side contains the expression or statement block. Lambda expressionsX => X * xRead as "X goes to X times X ".

 

In C # versions earlier than 2.0, the only way to declare a delegate is to use the naming method. C #2.0 introduces anonymous methods. In C #3.0 and later versions, lambda expressions replace anonymous methods as the preferred method for writing Inline code. However, the information about anonymous methods in this topic also applies to lambda expressions. In one case, the anonymous method provides functions not available in lambda expressions. The anonymous method allows you to omit the parameter list, which means you can convert an anonymous method to a delegate with various signatures. This is not possible for lambda expressions.

 

Example:

Delegate int additiondelegate (int A, int B); // delegate class
Class additionclass
{
   Int;
   Int B;
   Public additionclass (int A, int B)
   {
       This. A =;
       This. B = B;
   }

   Private int addition (int A, int B)
   {
       Return A + B;
   }

   Public int addition1 ()
   {
       Additiondelegate myadd = new additiondelegate (addition );
       Return myadd (A, B );
   }

   Public int addition2 ()
   {
       Additiondelegate myadd = delegate (int aa, int bb) {return AA + BB ;}; // anonymous method

       Return myadd (A, B );
   }
   Public int addition3 ()
   {
       Additiondelegate myadd = (AA, BB) => AA + BB; // Lambda expression

       Return myadd (A, B );
   }
}

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.