Linq--lambda expressions to know

Source: Internet
Author: User

In the process of learning LINQ, one of the most important stages is the familiarity with lambda use

1) Lambda Introduction

Lambda is essentially a delegate, and he is an anonymous delegate (anonymous function), which itself is developed from a functional programming language. Many lambda expressions use the lambda operator =>; the left side is the input parameter, the right side is the expression or the statement block, that is, the meaning of the assignment. Lambda typically appears as a delegate, assigning an expression directly to a delegate type, for example.


2) Illustrative examples

First we need to define a delegate:

Defines a delegate public        delegate string Deletransfer (string s);   Note Method signature

then the direct delegate points to the method, and executes just fine. Again we tried three methods, of course the third is the most commonly used also we need to learn:

Methods that need to be called in Method 1:

The method public string        topascal (string s)        {            return s.substring (0, 1) converts the lowercase letter of the string to uppercase. ToUpper () + s.substring (1). ToLower ();        }
private void Btnlambda_click (object sender, EventArgs e)        {            //lambda source '            //.net Framwork1.0   delegate-function pointer            //deletransfer trans = new Deletransfer (topascal);   Delegate-pointing method topascal            //console.writeline (trans ("ABCDTFDF"));           The result of the final output is the            //.net Framwork2.0 anonymous method performed by method topascal            //deletransfer trans = delegate (string s) {return S. Substring (0, 1). ToUpper () + s.substring (1). ToLower (); };            Console.WriteLine (Trans ("ABCDTFDF"));             The result of the final output is the            //.net Framwork3.5 lambda representation method performed by method topascal            Deletransfer trans = s and s.substring (0, 1 ). ToUpper () + s.substring (1,5). ToUpper ();            Console.WriteLine (Trans ("ABCDTFDF"));             The result of the final output is executed according to the method Topascal        }

in which we passed in a method of SubString, which is to intercept the string that needs to be displayed and then display it as required, as above, in the last method, I let it start from 0, specify a length of 1, uppercase display, starting from 1, specifying a length of 5, lowercase letters, Final output Result:

In the above code, the transfer value of trans is a lambda expression, which is simply a small example, but also to understand its appearance and use. Convenient use of LINQ.

Linq--lambda expressions to know

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.