C # 3.0 Feature lambda expressions

Source: Internet
Author: User

Attention: This article contains sample sources that refer to the Pro LINQ book from Joseph C. Rattz, Jr.

Lambda expressions are introduced by MS in C # 3.0. The lambda expression was first seen in the Lisp language, which was conceptualized by American mathematician Alonzo Church in 1936. This expression provides an easy way to describe the algorithm.

Before introducing lambda expressions, let's look at how an algorithm can be passed as a parameter to a method before that.

Using named methods (Named Methods)

Before C # 2.0, you can use a delegate (delegate) to do this work. For example, considering such an application, the array elements are filtered according to certain rules, and for common modules, they can be written as follows:

public class Common
{
  public delegate bool IntFilter(int i);

  public static int[] FilterArrayOfInt(int[] ints,IntFilter filter)
  {
    ArrayList aList = new ArrayList();
    foreach (int i in ints)
    {
      if (filter(i))
      {
        aList.Add(i);
      }
    }

    return ((int[]) aList.ToArray(typeof (int)));
  }
}

Then the algorithm person writes the filtering algorithm into a function, such as the following:

public class Application
{
  public static bool IsOdd(int i)
  {
    return ((i & 1) == 1);
  }
}

This isodd () method is the named approach that describes the algorithm. In the actual call process, caller calls the Filterarrayofint () method of the common class, and the second parameter of the method is the delegate (function pointer) of a filter algorithm. All filter algorithms must be of the same parameter and return value type. When the Filterarrayofint () method is invoked, the isodd () that describes the algorithm is used as an argument, and the event is taken as a proxy.

static void Main(string[] args)
{
  int[] nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  int[] oddNums = Common.FilterArrayOfInt(nums, Application.IsOdd);

  foreach (int i in oddNums)
  {
    Console.WriteLine(i);
  }
}

The result is, of course, 1, 3, 5, 7, 9. The implementation of the algorithm can write a variety of different filter, so the generation can be.

Using anonymous methods (Anonymous Methods)

In C # 2.0, anonymous methods are introduced, and code can be inline to replace delegates. The Oddnums assignment in the main () method in the example above can be rewritten as follows:

int[] oddNums = Common.FilterArrayOfInt(nums, delegate(int i) { return ((i & 1) == 1); });

This inline form can be simply used where the delegate parameter is, and is called an anonymous method because there is no method for defining a name. For code that does not require reuse, this method can greatly simplify the structure of the program, but reduces readability. For the more complex function body is very suck.

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.