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