Tuesday, June 9, 2009

New Features of dot Net 3.5 Framework

Dot net framework 3.5 is built on dot net 2.0 runtime library with addition of few dlls like: System.Core.dll, System.XML.LINQ.dll, System.AjaxExtension.dll etc..

Mainly enhancements are done with related to LINQ and Entitiy Model. Here are few enhancements listed:


1. Automatic Properties :

If you remember, prior to framework 3.5, we used to declare property within a class like this:

class Class1 {

privtae string _CustomerName;
public string CustomerName
{
get { return _CustomerName; }
set { _CustomerName = value; }
}
}


Now, we will see how to implement the same code in dot net 3.5:

class Class1 {
public string CustomerName {get; set;}
}





2. Object Initializers :

Another new feature coming with .NET 3.5, C# 3 and VB 9 is object initialization. Object Initializers allow you to pass in named values for each of the public properties that will then be used to initialize the object.

Suppose we have a class called School and few fields, we used to initialization the class like this:

School school1=new School();
school1.Name="ABC";
school1.Address="DEF";


Now the problem is that if we need to initialize different fields based on logic, we need to create different constructors based on our logic. Here comes the use of Object Initializers.

Object Initializers allow you to pass in any named public property to the constructor of the class. This is a great feature as it removes the need to create multiple overloaded constructors using different parameter lists to achieve the same goal.

So, from framework 3.5 onwards, we will use:

School school1=new School {
Name="ABC";
Address="DEF";
};





3. Collection Initializers :

Like Object Initializers, the new Collection Initializers allow you to create a collection and initialize it with a series of objects in a single statement. The following statement demonstrates how the syntax is very similar to that of the Object Initializers. Initializing a List is accomplished by passing the instances of the Customer objects wrapped inside of curly braces.

Getting this feature as part of framwork, we don not need to declare the collection first and then add items to it.


List SchoolList = new List
{
new School {ID = 101, SchoolName = "School1"},
new School {ID = 102, SchoolName = "School2"},
new School {ID = 103, SchoolName = "School3"}
};




4. Extension Method

Extension methods are a new feature that allows you to enhance an existing class by adding a new method to it without modifying the actual code for the class. This is especially useful when using LINQ because several extension methods are available in writing LINQ query expressions.

For more details on extension method, check "What is Enxtension Method "

Extension Methods are very useful when you cannot add a method to the class itself, as in the case of creating a Cube method on the int class. Just because you can use a tool, does not mean you should use a tool.




5. Anonymous Types

When you create an Anonymous Type you need to declare a variable to refer to the object. Since you do not know what type you will be getting (since it is a new and anonymous type), you can declare the variable with the var keyword. This technique is called using an Implicitly Typed Variable.

Chek more details on "All about Anonymous Types

4 comments:

Manab Ranjan Basu said...

Very good post,its really helpful,for more details you can browse
http://portal-management.blogspot.com/2009/05/c-30-tutorial-1.html
http://portal-management.blogspot.com/2009/05/c-30-tutorial-1extension-methods.html
http://portal-management.blogspot.com/2009/06/c-30-tutorial-3lambda-expressions.html
http://portal-management.blogspot.com/2009/06/c-30-tutorial-4object-initializers.html
http://portal-management.blogspot.com/2009/06/c-30-tutorial-5anonymous-types.html
http://portal-management.blogspot.com/2009/06/c-30-tutorial-6type-equivalence.html
http://portal-management.blogspot.com/2009/06/c-30-tutorial-7projections.html
http://portal-management.blogspot.com/2009/06/c-30-tutorial-8linq.html

Sambeni said...

Very nice post. I personally like this post.
.NET application development services

buy domain in india said...

Great blog, helped me a lot, keep sharing, thanx…

Albert Andrada said...

Really Nice Information,Thank You Very Much For Sharing.
Web Development Company