What is New In C# 4.0
Dynamic lookup
dynamic d = 7;
d = “Abdel-Rahman”;
Named and optional parameters
public void M(int x, int y = 5, int z = 7);
M(1, 2, 3); // ordinary call of M
M(1, 2); // omitting z – equivalent to
M(1, 2, 7)
M(1); // omitting both y and z – equivalent to M(1, 5, 7)
M(1, z: 3); // passing z by name
M(x: 1, z: 3); // passing both x and z by name
M(z: 3, x: 1); // reversing the order of arguments
COM specific interop features
This means that you can easily access members directly off a returned object, or you can assign it to a strongly typed local variable without having to cast. To illustrate, you can now say
excel.Cells[1, 1].Value = "Hello";
// instead of
((Excel.Range)excel.Cells[1, 1]).Value2 = "Hello";
Excel.Range range = excel.Cells[1, 1];
// instead of
Excel.Range range = (Excel.Range)excel.Cells[1, 1];
Variance
// ToDo : 1.0 Add Example.
What is New In C# 3.0 , C# 3.5
you can see my old post about C# 3.0 here , but i think i ll write about it here in more details.
Automatic Properties
public int MyProperty { get; set; }
Implicitly Typed Local Variables
var n = 5;
var s = “LINQ rules”;
var b = new[] { 1, 1.5, 2, 2.5 };
var c = new[] { "hello", null, "world" };
Anonymous Types
var anonType = new {X = 1, Y = 2};
var NewTempClass = new {FirstName = "name" , Age = 32 };
string x = NewTempClass.FirstName;
Object Initializes
Contact contact = new Contact { LastName = “Magennis”, Age = 9 };
Collection Initializers
List<int> digits = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
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
public static int NewExtensionMethod(this string s) {return Int32.Parse(s); }
Partial Methods
Query Expressions
var result = from item in List.items Where item.Title = ”Abdel”
select item;
Lambda Expressions
IEnuerable <Person> results = People.Where( P => P.LastName ="Abdel");
double Averageage = People.Average(P => P.Age);
Expression Trees
What is New In C# 2.0
Generics
class BaseNode { }
class BaseNodeGeneric<T> { }
class NodeConcrete<T> : BaseNode { }
class NodeClosed<T> : BaseNodeGeneric<int> { }
class NodeOpen<T> : BaseNodeGeneric<T> { }
class BaseNodeMultiple<T, U> { }
class Node4<T> : BaseNodeMultiple<T, int> { }
class Node5<T, U> : BaseNodeMultiple<T, U> { }
class SuperKeyType<K, V, U> where U : System.IComparable<U> where V : new() { }
static void Swap<T>(ref T lhs, ref T rhs)
class SampleClass<T>
{
void Swap(ref T lhs,
ref T rhs) { }
}
Iterators
-
An iterator is a section of code that returns an ordered sequence of values of the same type.
-
An iterator can be used as the body of a method, an operator, or a get accessor.
-
The iterator code uses the yield return statement to return each element in turn. yield break ends the iteration. For more information, see yield.
-
Multiple iterators can be implemented on a class. Each iterator must have a unique name just like any class member, and can be invoked by client code in a foreach statement as follows: foreach(int x in SampleClass.Iterator2){}
-
The return type of an iterator must be IEnumerable, IEnumerator, IEnumerable<T>, or IEnumerator<T>.
The yield keyword is used to specify the value, or values, returned. When the yield return statement is reached, the current location is stored. Execution is restarted from this location the next time the iterator is called.
Iterators are especially useful with collection classes, providing an easy way to iterate non-trivial data structures such as binary trees.
Partial Classes
Nullable Types
example int? x = null; int y = x ?? -1;
Use the System.Nullable.GetValueOrDefault
The syntax T?
Anonymous Methods
Del d = delegate(int k) { /* … */ };













Leave a Reply