C#3.0 has been introduced for some time, for various reasons, has not been to learn, these two days in the garden to see Lao Zhao's Save C # 2.0, but we really do it? It mentions some of the new features and advantages of c#3.0. It reminds me of my interest in 3.0, to study and to share with the novice.
In c#2.0, Microsoft has brought us some new features, such as generics, anonymous delegates, and so on. However, these new features give people a sense of "copying" from other languages (such as generics like C + + templates, and some features similar to something in Java). But in c#3.0, some of the new features that Microsoft has brought to me may be features that all previous development languages did not have. This undoubtedly greatly embodies the strong advantage of c#3.0 in the development of language.
Lambda expression
A LAMBDA expression is an anonymous function that can contain expressions and statements and can be used to create delegates or expression tree types. All lambda expressions are => using the lambda operator. For a more detailed explanation of the lambda you can refer to MSDN. It's very clear in that.
Here's a simple example to illustrate the benefits of a lambda. Lambda provides a clearer way to implement anonymous delegation processing. For example, in 2.0. We can write code like this:
public class Example
{
public static void Demo (System.Windows.Controls.TextBlock outputBlock)
{
Func<string, string> convert = delegate(string s)
{ return s.ToUpper(); };
string name = "Dakota";
outputBlock.Text += convert(name) + "\n";
}
}
Use func< (The < T, tresult>) >) delegate in C # with anonymous methods.
In 3.0, we can use the lambda to more clearly pass the parameters:
public class Example
{
public static void Demo(System.Windows.Controls.TextBlock outputBlock)
{
Func<string, string> convert = s => s.ToUpper ();
string name = "Dakota";
outputBlock.Text += convert(name) + "\n";
}
}
The underlying type of a LAMBDA expression is one of the generic Func delegates. This allows a lambda expression to be passed as an argument without explicitly assigning it to a delegate. In particular, because many type methods in the System.Linq namespace have func< (of < (T, tresult>) >) parameters, you can pass lambda expressions to these methods without explicitly instantiating func< (of &L t; (t, tresult>) >) delegates. This will make our code more concise and logically easier to understand.