Directory
Write in front
Anonymous methods
An example
Lambda
Defined
An example
Summarize
Reference Text Chapters
Write in front
New Year's Day three days at home idle, looked at the relevant content of LINQ, but also prepare a systematic study, as a prelude to Learning LINQ, or first to talk about the lambda and anonymous methods of knowledge points. is also a knowledge of the leak, perhaps you will say that there is no big deal, the project is in use, but some knowledge, you look back at the time, there will always be a little different harvest, this I am empathy, I read a habit, a book, I can see a three or four times, every time there will be a harvest. Of course, you can say that at that time certainly did not look seriously, is not like that, I think the most direct reason is that at that time you look at, did not meet in the real project, so you have no deep understanding of it, if there is so little project experience, in the reading of the theoretical knowledge, there will always be the kind of enlightened feeling. Believe it or not, I believe it anyway!
Anonymous methods
Anonymous methods as the name implies, there is no Name method, but still have the method body, still can work. In many places you may have seen it, such as JS, with the most!
Take a look at MSDN to say:
In the C # version prior to 2.0, the only way to declare a delegate was to use a named method. C # 2.0 introduces anonymous methods, whereas in C # 3.0 and later, LAMBDA expressions replace anonymous methods as the preferred way to write inline code. However, the information in this topic about anonymous methods also applies to LAMBDA expressions. In one case, an anonymous method provides functionality that is not available in a LAMBDA expression. You can use anonymous methods to ignore parameter lists. This means that anonymous methods can be converted to delegates with various signatures. This is not possible for Lambda expressions.
An example
1 namespaceWolfy.linqdemo2 {3 /// <summary>4 ///create a delegate del5 /// </summary>6 /// <param name= "x" >Parameters</param>7 Public Delegate voidDel (intx);8 class Program9 {Ten Static voidMain (string[] args) One { A //create a delegate object using an anonymous method D -Del d =Delegate(intx) - { the Console.WriteLine (x); - }; - } - } +}
by using anonymous methods, you reduce the coding overhead required to instantiate a delegate because you do not have to create a separate method.
For example,
1 New Thread (2 Delegate ()3 {4 // method Body 5 Console.WriteLine ("HelloWorld"); 6 });
It can be more straightforward to understand that if the method is used once, then the anonymous method can be used.
Lambda definition
A LAMBDA expression is an anonymous function that you can use to create a delegate or an expression tree type.
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.
An example
1 /// <summary>2 ///create a delegate del3 /// </summary>4 /// <param name= "x" >Parameters</param>5 Public Delegate voidDel (intx);6 class Program7 {8 Static voidMain (string[] args)9 {TenDel del = x = Console.WriteLine (x *x); OneDel2);//4 A Console.read (); - } -}
To create an expression tree, you can:
1 /// <summary>2 ///create a delegate del3 /// </summary>4 /// <param name= "x" >Parameters</param>5 Public Delegate voidDel (intx);6 class Program7 {8 Static voidMain (string[] args)9 {TenDel del = x = Console.WriteLine (x *x); OneDel2);//4 Asystem.linq.expressions.expression<del> Expression = x = =Console.WriteLine (x); - Console.read (); - } the}
The above example creates an expression directory tree object expression, because the Del delegate is not a return value, here is the direct output.
An expression lambda
The lambda expression that the expression is on the right side of the = = operator is called expression Lambda. The expression lambda returns the result of an expression and takes the following basic form:
(input parameters) = Expression
Note: parentheses are optional only if the lambda has only one input parameter, otherwise parentheses are required. Two or more input parameters in parentheses are delimited by commas :
1 (x, y) = x = = Y
1 // Sometimes, it is difficult or impossible for the compiler to infer the input type. If this is the case, you can explicitly specify type 2 (intstring s) as shown in the following example =-s.length > x3 // use an empty parenthesis to specify 0 input parameters 4 () = SomeMethod ()
Statement Lambda
Statement Lambda is similar to an expression lambda expression except that the statement is enclosed in curly braces
1 (input parameters) = {statement;}
Statement (statement lambda) can contain any number of statements, but usually no more than two or three.
An example
1 namespaceWolfy.linqdemo2 {3 /// <summary>4 ///create a delegate del5 /// </summary>6 /// <param name= "x" >Parameters</param>7 Public Delegate voidDel (stringstrName);8 9 class ProgramTen { One Static voidMain (string[] args) A { -Del D = x = = - { the strings ="Hello"+" "+x; - Console.WriteLine (s); - }; -D"Wolfy"); + Console.read (); - } + } A}
Attention
Like anonymous methods, statement lambda cannot be used to create an expression tree
Summarize
Here's a brief introduction to lambda and anonymous methods. Although it is often used in projects, the basics of lambda still need to be mended.
Reference articles
Http://msdn.microsoft.com/zh-cn/library/0yw3tz5k.aspx
Http://msdn.microsoft.com/zh-cn/library/bb397687.aspx
A preliminary understanding of the lambda Expression of LINQ