Automatic Properties
generates a temp private variables to assign properties
Old Code
private int myProperty;
public int MyProperty {
get{return myProperty} ;
set {myProperty = value }; }
New Code
public int MyProperty { get; set; }
Implicitly Typed Local Variables
Replaced the intializers (string,int,..) to be of general type Var how ever any defined var variable must be assigned to value from which the compiler knows the type of the new declared variable
Old Code
int n = 5;
string s = “LINQ rules”;
int[] nums = new int[] {1, 2, 3};
New Code
var n = 5;
var s = “LINQ rules”;
var nums = new int[] {1, 2, 3};
Implicitly Typed Local Variables
Old Code
int[] a = new int[] { 1, 10, 100, 1000 };
double[] b = new double[] { 1, 1.5, 2, 2.5 };
string[] c = new string[] { "hello", null, "world" };
New Code
var a = new[] { 1, 10, 100, 1000 };
var b = new[] { 1, 1.5, 2, 2.5 };
var c = new[] { "hello", null, "world" };
Anonymous Types
A New Fast way of declaring types.
Old Code
class ConcreteType {
private int _x;
private int _y;
public int X { get { return _x; } set { _x = value; } }
public int Y { get { return _y; } set { _y = value; } }
}
ConcreteType conType = new ConcreteType();
contype.X = 1; contype.Y = 2;
New Code
var anonType = new {X = 1, Y = 2};
or
var NewTempClass = new {FirstName = "name" , Age = 32 };
Object Initializers
A New Fast way of intializing objects .
Old Code
Contact contact = new Contact();
contact.LastName = “Magennis”;
contact.DateOfBirth = new DateTime(1973,12,09);
New Code
Contact contact = new Contact { LastName = “Magennis”, DateOfBirth = new DateTime(1973,12,09) };
Collection Initializers
A New Fast way of intializing Collections and list.
Old Code
List<int> digits = new List<int> ();
digits.add(0);
digits.add(1);
digits.add(2);
....
New Code
List<int> digits = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
more over
List<Contact> contacts = new List<Contact>
{ new Contact {LastName = “Doherty”, DOB =55},
new Contact {LastName = “Wilcox”, DOB = 66} };
Extension Methods
They made a new generic methods so you can extend the functionality of any class you want.
New Code
using Acme.Utilities;string Str = "New Test";
int i = Str.NewExtensionMethod();
namespace Acme.Utilities
{
public static class Extensions
{
public static int NewExtensionMethod(this string s)
{
return Int32.Parse(s);
}
}
}
Partial Methods
Old Code
ToDo:
New Code
TODO:
Query Expressions
Old Code
ArrrayList myquery = new ArrayList();
string[] Files = Directory.GetFiles(@"c:\");
foreach(string Str in Files)
{
if(Str.EndWith(".Bat")
myquery.add(str);
}
New Code
var myquery = from i in Directory.GetFiles(@"c:\")
Where i.EndWith(".Bat")
Selecet i;
Lambda Expressions
Old Code
IEnuerable <Person> results = People.Where(
delegate(Person P)
{return P.LastName = "Abdel";}
);
double Averageage = People.Average(
delegate(Person P)
{return P.Age;}
);
New Code
IEnuerable <Person> results = People.Where( P => P.LastName ="Abdel");
double Averageage = People.Average(P => P.Age);
Expression Trees
Old Code
ToDo:
New Code
TODO:
RESOURCES :













[...] can see my old post about C# 3.0 here , but i think i ll write about it here in more [...]