Hi there! Seems you are visiting my site for the first time. Hmm, Why dont you subscribe to RSS or use email subscription service to get all posts at your inbox (No spam). You can also follow me as google friend.
Please carry on reading my blogs, if you like, please give your feedback too. Have a wonderful day! (Dismiss)

Monday, August 31, 2009

New Features of C# 3.0

1. Extension Methods : You can define extension methods to your namespace, so that if this is added, the class in which it is applied will act as instance variable. Example :


pubic void isDateTime(this string x)
{
return DateTime.TryParse(x);
}

The function will be added to the string object, and you can use it normally as with instance methods of String class.

2. Annonymous Types : You can create unnamed objects that is not derived from a class, rather it is created on a fly. Annonymous types comes very handy when working with LINQ.
Example :

var x = new {x=20,y=40};


thus x will have 2 properties x and y.

3. Initialisers : While you create object of a class, ,we now dont need to call its constructors as all the public properties can be initialised easily.
MyClass x = new MyClass {
MyProperty1 = 20,
MyProperty2 ="This is new Property "};

thus we can set properties directly without calling the respective constructors. This feature also comes very handy when we dont want to have lots of constructors overloads or just say we have to create 2 constructors with same set of parameters but which will be assigned to 2 different properties . Accessing properties directly is very handy feature lately.

Also there is collection initialisers like
List mycol = {"ss","gg"};
You can do this without calling add method.


4. Implicitely Typed Variables : C# introduces "var" which means when the object is assigned the type will be determined automatically. if you assing a string to x it will be of string type, or whatever you do.

5. Partial Methods : .NET 3.5 introduces partial methods, where the same methods can be declared more than once so that when called it will automatically add features to the existing method that is already defined.
Note : Partial methods must not return anything, it should be defined as Void.

6. Property implementation is not required : Sometimes we just need to define properties to expose objects. In such case we dont need to implement a property, rather we can go with auto implementation feature introduced.

public string myproperty {get;set;};

The property will implicitly create a private variable and assign the values properly.

7. LINQ and Lambda Expression : You can query .net objects now using Linq. Lambda expressions are short - hand representation of LINQ queries. :)

This is just basics of what introduced in C# 3.0. I will discuss more on each of them when I find time.

Hope you like reading this.

1 comment:

Unknown said...

Good info, love it...keep posting more on c#.