.NET 3.5 Language Enhancements

.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.

.NET Language Enhancements

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.

.NET Language Enhancements
Object Initializer

When we initiate the above order details class, the common method to pass the value to public member of the class is shown below:

.NET Language EnhancementsIn 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.

.NET Language EnhancementsCollection Initializer

The Collection Initializer is similar to Object Initializer. The method to add values to Generic List<T> Type Collection is below

.NET Language Enhancements

With the Collection Initializer enhancement, the above code can be rewritten as below

.NET Language Enhancements

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

.NET Language Enhancements

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

.NET Language EnhancementsNow CLR String type contains  “ShowWelcomeMessage” method. If you pass string value as “Praba” , the return message as “Welcome User: Praba”

.NET Language Enhancements

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).

.NET Language Enhancements

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:

.NET Language Enhancements

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.

.NET Language Enhancements

The same code will be written in Lambda expression as below.

.NET Language Enhancements

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,

.NET Language Enhancements

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,

.NET Language EnhancementsBased 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

, , ,

Open chat
1
Hello
Can I help you?