Write in front
I was idle at home on the three days of New Year's Day, so I looked at Linq's related content and prepared to study it systematically. As a prelude to learning Linq, I have to talk about the knowledge points of Lambda and anonymous methods first. It can also be considered as checking for missing knowledge points. Maybe you will say that this is no big deal and is used in projects. However, when you look back and check some knowledge, you will always have a little different gain. I empathize with this. I have a habit of reading books. I can read a book three or four times, and there will always be gains every time. Of course, you can say that you definitely didn't look at it seriously at that time, and it was not like that. I think the most direct reason is that you looked at it at that time, and you had never encountered it in a real project, so you don't have a deep understanding of it. If you have such a little project experience and read the theoretical knowledge in the book, you will always feel that you suddenly feel enlightened. Believe it or not, I believe it anyway!
Anonymous method
As the name suggests, an anonymous method is a method without a name, but it still has a method body and can still work. You may have seen it in many places, such as JS, which is used the most!
Let’s see what Msdn said:
In C# versions prior to 2.0, the only way to declare a delegate is to use a named method. C# 2.0 introduces anonymous methods, while in C# 3.0 and later, Lambda expressions replace anonymous methods as the preferred way to write inline code. However, the information about anonymous methods in this topic also applies to Lambda expressions. In one case, anonymous methods provide functionality not found in Lambda expressions. You can use anonymous methods to ignore the parameter list. This means that anonymous methods can be converted to delegates with various signatures. This is impossible for Lambda expressions.
namespace
{
/// <summary>
/// Create a delegate Del
/// </summary>
/// <param name="x">parameters</param>
public delegate void Del(int x);
class Program
{
static void Main(string[] args)
{
//Create a delegate object using anonymous method
Del d = delegate(int x)
{
(x);
};
}
}
}
By using anonymous methods, the encoding system overhead required to instantiate a delegate is reduced because you don't have to create separate methods.
For example, a thread class can create a thread and contain code executed by that thread.
Thread thread = new Thread(
delegate()
{
//Method
("Hello world");
});
It can be understood more bluntly that if the method is used then anonymous method can be used.
Lambda
definition
A Lambda expression is an anonymous function that can be used to create a delegate or expression directory tree type.
To create a Lambda expression, you need to specify the input parameter (if any) on the left side of the Lambda operator =>, and then enter the expression or statement block on the other side.
/// <summary>
/// Create a delegate Del
/// </summary>
/// <param name="x">parameters</param>
public delegate void Del(int x);
class Program
{
static void Main(string[] args)
{
Del del = x => (x * x);
del(2);//4
();
}
}
To create an expression directory tree, you can:
/// <summary>
/// Create a delegate Del
/// </summary>
/// <param name="x">parameters</param>
public delegate void Del(int x);
class Program
{
static void Main(string[] args)
{
Del del = x => (x * x);
del(2);//4
<Del> expression = x => (x);
();
}
}
The above example creates an expression directory tree object expression. Since the Del delegate does not return value, it is output directly here.
Expression Lambda
The lambda expression on the right side of the => operator is called the "expression lambda". The expression lambda returns the result of the expression and takes the following basic form:
(input parameters) => expression
Note: Parenthes are optional only if lambda has only one input parameter; otherwise brackets are required. Two or more input parameters in brackets are separated by commas:
(x, y) => x == y
// Sometimes, the compiler has difficulty or cannot infer input type. If this happens, you can explicitly specify the type as shown in the following example
(int x, string s) => > x
//Use empty brackets to specify zero input parameters
() => SomeMethod()
Statement Lambda
The statement lambda is similar to the expression lambda expression, except that the statement is enclosed in braces.
(input parameters) => {statement;}
statement (statement Lambda) can contain any number of statements, but usually not more than two or three.
An example
namespace
{
/// <summary>
/// Create a delegate Del
/// </summary>
/// <param name="x">parameters</param>
public delegate void Del(string strName);
class Program
{
static void Main(string[] args)
{
Del d = x =>
{
string s = "Hello" + " " + x;
(s);
};
d("wolfy");
();
}
}
}
Notice
Like anonymous methods, statement lambda cannot be used to create expression directory trees.
Summarize
Here is a brief introduction to Lambda and the anonymous method. Although it is often used in projects, the basic knowledge of Lambda still needs to be supplemented.