Tuesday, June 1, 2010


Me : Do you want to jump to C# 4.0?
Someone : Yes !!!
Me : Wait !!! did  you know the new features  that were in C# 3.0 when it was released?
Someone: No


Ok read the following:-


1. Implicitly typed local variables and arrays:  A new keyword is introduced called var. This allows you to declare a variable and let the compiler figure out the type

 

ex:
  1.         // Declare a variable of type int
  2. var i = 5;
  3. // Declare an int array
  4. var a = new[] { 1, 10, 100, 1000 };
  5. // Usage:
  6. for (var i = 0; i < 5; i++) { }
  7. foreach (var item in MyList) { }
  8. using (var file = new StreamReader("Filename.txt")) { }


2. Auto-implement properties: Use this when no additional logic is required in the property accessors.

 

ex:
  1.         // Previously we had to do this,
  2.         private string _name;
  3.         public string Name
  4.         {
  5.             get { return this._name; }
  6.             set { this._name = value; }
  7.         }
  8.         // Now we can shorthand it, there’s no need to declare the _name variable
  9.         public string Name { get; set; } // The compiler creates a private anonymous backing field


3. Object and collection initializers: Set properties inline during object creation.
   

ex:
  1.     public class A {
  2.     public string Name { get; set; }
  3.     public string Address { get; set; }
  4. }
  5. // Previously we would have to do,
  6. A a = new A();
  7. a.Name = "...";
  8. a.Address = "...";
  9. // Now we can do this,
  10. A a = new A { Name = "...", Address = "..." };
  11. // Similarly, we can initialize collections,
  12. List<int> MyList = new List<int> { 0, 1, 2, 3, 4 };


4. Extension methods: Now you can add methods to existing types without creating a new derived type, recompiling or modifying the original type’s code. This is incredibly useful in writing cleaner code,

 

ex:
  1. // Previously we would do this,
  2. String domain = GetDomain(url); // url is a string type
  3. // Now we can do this,
  4. String domain = url.GetDomain();  // String now has a custom method ...
  5. // ... because we defined an extension method for the string type,
  6. public static class MyStringExtensions
  7. {
  8.     public static String GetDomain(this String str)
  9.     {
  10.         // Extract domain from url and return;
  11.     }
  12. }


5. Anonymous types: If you need a small class with read only properties, you can now use a simple syntax,
    

ex:
  1. // a is an anonymous type, the compiler creates the class with
  2. // private string fields _name and _address
  3. // and public string properties Name and Address,
  4. var a = new { Name = "...", Address = "..." };
  5. // a.Name and a.Address are read-only,
  6. string s = a.Name;


6. Query keywords (LINQ): We are now able to use TSQL like syntax inside C# to query data sources (could be objects, collections, XML, or SQL databases). Introduces a host of new keywords like from, where, select, group, orderby, join, etc.
    

ex:
  1. // The data source,
  2.      int[] numbers = { 0, 1, 2, 3, 4, };
  3.      // Create the query,
  4.      var lessthan3 =
  5.          from num in numbers
  6.          where num < 3
  7.          select num;
  8.      // Output: 0 1 2
  9.      foreach (var m in lessthan3) {
  10.          Console.Write(m.ToString() + " ");
  11.      }


8. Partial method definitions: A partical class (introduced in C# 2.0) can now have partial methods. This simply means the method signature is in one partial class and the implementation in another. There are some constraints, like the method is implicitly private (and therefore cannot be virtual) and must return void.
     

ex:
  1. public partial class A
  2. {
  3.     // Method definition
  4.     partial void Method();
  5. }
  6. public partial class A
  7. {
  8.     // Method implementation
  9.     partial void Method() { /* Do Work */ }
  10. }

 

 

C# fast-food by:
Eng.Waleed abou-zaid
Senior Software Developer
Harf Information Technology
MCSD.Net,MCAD.Net,MCP,A+,N+ certified
walid.abouzaid@gmail.com



0 comments: