.NET 3.5 Language Enhancements
Microsoft .NET Development has been enhancing and upgrading its language framework since its inception. There seems a tremendous upgrade from .NET 2.0 Framework to .NET 3.5 framework, not just with introducing new sets of API but also in terms of dealing with objects, properties and methods. Below are a list of such language enhancements that are explained in brief in this article:
- Automatic Properties
- Object Initializer
- Collection Initializer
- Extension Methods
- Anonymous Methods
- Lambda Expressions
- Anonymous Types
Automatic Properties:
The sample shows a class named OrderDetails which has properties like OrderID,OrderPrice and OrderShippingAddress.
In the Automatic Property enhancement feature, the above code can be rewritten as below. The flexibility of this is, you don’t need to implement get, set property. At runtime Automatic Properties can automatically create private declaration for your values and implement get and set properties automatically.
When we initiate the above order details class, the common method to pass the value to public member of the class is shown below:
In the Object Initializer property enhancement feature, the above value passing code can be rewritten as below. The flexibility of this is,you don’t need to explicitly invoke constructor.
The Collection Initializer is similar to Object Initializer. The method to add values to Generic List<T> Type Collection is below
With the Collection Initializer enhancement, the above code can be rewritten as below
Extension Methods
You can write your own method to any existing Public CLR Type. These methods are known as Extension Methods.
For example we can add a new method named “ShowWelcomeMessage” to “String” CLR Type. The Method code will be as below
The above methods show that entered UserName can be concatenated with “Welcome User” string and the concatenated message can be returned. The keyword “this” tells the compiler to add the “ShowWelcomeMessage” method to CLR Type “String”(which will come after the “this” keyword in above method) .
We include the NameSpace named “ExtensionMethodNamespace” in our aspx file by “Using” keyword as shown below
Now CLR String type contains “ShowWelcomeMessage” method. If you pass string value as “Praba” , the return message as “Welcome User: Praba”
If you add an extension method to any base CLR Type, it reflects to derived CLR types which are derived from that base type. For example you can add a method to Object CLR type, it will affect the types derived from Object like String,Int etc.
Anonymous Method
Anonymous methods are inline unnamed method in your code. Let us see an example of Copy values from one string array to another string array (which are values that start with ”U” from the first string array are copied into second string array).
The above code you can find “isStartswithU” method to determine string value is starts with U or not. With the Anonymous method feature, you can code it inline instead of writing a separate method as below:
Lambda Expression
Lambda expression is mainly used in LINQ. It makes the code is easier for writing anonymous methods. Lambda expressions are similar to anonymous methods. Example of anonymous methods are below.
The same code will be written in Lambda expression as below.
In the above code P=>P.OrderID is an Lambda expression. One difference in Lambda expression is, you don’t need to declare P. The compiler automatically will declare P based on the before the where condition variable. The advantage in Lambda expression is to make the code easier to write it.
Anonymous Types
Anonymous types means that you don’t need to specify type. It should be created with the “New” keyword. For example,
You don’t need to specify the amount type. Compiler automatically infers the anonymous type of the properties.
The var keyword is mainly used in anonymous types. Var keyword is implicitly typed variable. For example,
Based on the values assigned, the compiler knows that “FirstName” is String Type and “Age” is Integer type. Find .NET Developers who can customize all your needs using .NET framework