Tuesday, November 24, 2009

C# 3.0 features

  1. 3.0 allows properties without local variables
  2. var  (varient) - it is implicitly typed variable
  3. Anonymous variable
  4. Lamda expression
  5. FUNC<>
  6. Extention Method

Here is the detail:

1. C#3.0 allows properties without local variables: 
Tip: Type prop in the visual studio and press tab
 2.  Simple and implicitly typed local variable

  • It is implicitly typed variable
  • var i=10;
  • var str="nidhish";
  • var x="109"; // It is invalid. Compiler does not allow this.
  • var CANNOT be used as a parameter or a return type. It is heavily used in LINQ
3. Anonymous variable:

  • it is helpful for object initialization
  • Example:
List mystring=new List {
  "Item1",
  "Item2",
  "Item3"}
4. Lamda expression:

  • Example:
delegate long myadd(int n1,int n2);
Public void TestLamda()
{
   myadd ad= (n1,n2) => { return n1+n2; };
   Console.WriteLine(ad.invoke(100,150));
}

5. Extending type with extension method

Extension method provide developers to extend the functionality of an existing type. Extension methods are static methods with keywork this

Eg: Extend string class with a reverse method

    No comments: