In C# 2.0, Microsoft has brought us some new features, such as generics, anonymous delegation, etc. However, these new features will more or less give people the feeling of "copying" from other languages (for example, generic templates similar to C++, and some features are similar to some things in Java). But in C# 3.0, some new features that Microsoft brought to me may be features that were not available in all development languages before. This undoubtedly greatly reflects the powerful advantages of C# 3.0 in the development language.
Lambda Expressions
A Lambda expression is an anonymous function that can contain expressions and statements and can be used to create delegate or expression directory tree types. All Lambda expressions use the Lambda operator =>. For more detailed explanations about Lambda, you can refer to MSDN. It is said very clearly.
Here is a brief example to illustrate the benefits of Lambda. Lambda provides a clearer implementation in handling anonymous delegations. For example in 2.0. We can write code like this:
publicclassExample { publicstaticvoidDemo() { Funcconvert=delegate(strings) {();}; stringname="Dakota"; +=convert(name)+"n"; } }
Use Func<(Of <(T, TResult>)>) delegate in C# with anonymous methods.
In 3.0, we can use Lambda to pass parameters more clearly:
publicclassExample { publicstaticvoidDemo() { Funcconvert=s=>(); stringname="Dakota"; +=convert(name)+"n"; } }
The underlying type of the Lambda expression is one of the generic Func delegates. This allows the lambda expression to be passed as arguments without explicitly assigning it to the delegate. In particular, because many typed methods in the namespace have Func<(Of <(T, TResult>)>)) parameters, you can pass lambda expressions to these methods without explicitly instantiating the Func<(Of <(T, TResult>)>) delegate. This can make our code more concise and logically easier to understand.
Initialization of objects
In C#, some improvements have been made to the initialization of objects. A new feature is to provide more convenient syntax rules to declare the value of a variable.
Suppose we declare a Student object:
publicclassStudent { privatestring_stuName; privatestring_stuAge; privateint_stuClass; publicStudent(){} publicstringStuName { get{return_stuName;} set{_stuName=value;} } publicstringStuAge { get{return_stuAge;} set{_stuAge=value;} } publicintStuClass { get{return_stuClass;} set{_stuClass=value;} } }
In C# 2.0, we declare variables and assign values like this:
Studentstu=newStudent(); ="Brian"; ="21"; ="Class 1";
And in C# 3.0, we can initialize the object like this:
Studentstu2=newStudent { StuName="Brian", StuAge="21", StuClass="Class 1" };
It is not difficult to see from the code that C# 3.0 provides us with a very convenient way to initialize objects.
Query
I believe everyone should have heard of this, that is the famous Linq. This is one of the most unique and useful new features in C# 3.0. Linq has changed the way we write data applications. Previously, developers needed to consider and write unnecessary code to process data in different data sources (SQL Server, XML, Memory......). LINQ helped us solve this annoying problem very well. At the same time, with the help of Lambda, we can more conveniently and accurately query the data we want.
Simple data query example using Linq:
privatevoidBindGridView(stringcriteria) { stringstrConn=["connstr"].ConnectionString; NorthwindDbdb=newNorthwindDb(strConn); IEnumerableresults; if(criteria==) { results=(); } else { results=( (criteria) selectc).ToArray(); } =results; (); }
Variable declaration
What I want to talk about here is var. var is a keyword provided in C# 3.0 for declaring variables. Developers can declare variables without considering the type of variables (this is very similar to Javascript). But there are still some differences between the two.
Similarity: Use var to declare any type of local variable.
Difference: It is only responsible for telling the compiler that the variable needs to infer the type of the variable based on the initialization expression, and can only be a local variable.
We can declare variables like this:
vari=10; varname="edisundong"; varnumbers=newint[]{1,2,3};
var is just a keyword. It is not a new type in C# 3.0. It is responsible for telling the compiler that the variable needs to infer the type of the variable based on the initialization expression. The above statement is equivalent to
inti=10; stringname="edisundong"; int[]numbers=newint[]{1,2,3};
There are a few more things to note here:
1. Values must be assigned at the same time when declaring.
2. After declaring a local variable with var, it still has strong types.
varinteger=10;
integer="edisundong";
A Cannot implicitly convert type string to int error will be reported during compilation.
3. The compile-time type of the initializer expression cannot be of a null type.
4. The declaration of var is limited to local variables
Extended Method
In the past, if we wanted to extend the function of a class, we had to directly originate from it and learn the method, in C# 3.0, a very quick method of extending the function was introduced.
publicstaticclassStudentExtensionMethods { publicStudentExtensionMethods() { // //TODO: Add constructor logic here// } publicstaticstringGetStudentInformation(thisStudentstu) { ("Name:{0}{1}Age:{2}",, ,); } }
Define a class, which defines a method. Note: both this class and method are static, and the parameters of the method are class Student. In this way, the Student class can extend the GetStudentInformation method:
Studentstu2=newStudent { StuName="Brian", StuAge="12", StuClass="Class 1" }; (());
Summary: After learning C# 3.0, I feel that it has brought a lot of surprises, and many new features are unknown before. The new features of C# 3.0 should be more than that, and we need to continue learning and research.