C#

C# 3.0 Features

1. Implicitly Typed Local Variables
2. Extension Methods
3. Lambda Expressions
4. Object and Collection Initializes
5. Anonymous Types
6. Implicitly Typed Arrays
7. Query Expressions
8. Expression Trees
9. Automatically Implemented Properties

Automatically Implemented Properties:

Example:

In C# 2.0 :

public Class Position {
private int x;
private int y;
public int X { get { return x; } set { x = value; } }
public int Y { get { return y; } set { y = value; } }
}

In C# 3.0 :

public Class Position {
public int X { get; set; }
public int Y { get; set; }
}

• Both accessors(get,set) must be present and both must have semicolon bodies . e.g. public int X{get;} -- Not possible
• Can have different accessibility modifiers. e.g. public int X{get; private set;} -- possible
• Can have different accessibility modifiers e.g
• Definite assignment of struct types with auto-implemented properties can only be achieved using the standard constructor of the struct, since assigning to the property itself requires the struct to be definitely assigned.


Implicitly Typed Local Variables:

It means we don’t need to define the type of variable, the type of the local variable being declared is inferred from the expression used to initialize the variable.

Example:

In C# 2.0 :

int i = 5;
string s = "Hello";
double d = 1.0;
int[] numbers = new int[] {1, 2, 3};
Dictionary orders = new Dictionary();

In C# 3.0 :

var i = 5;
var s = "Hello";
var d = 1.0;
var numbers = new int[] {1, 2, 3};
var orders = new Dictionary();

Restrictions:
• The declarator must include an initializer e.g var x; ( Error Not permitted)
• The initialize must be an expression. E.g var x = {1,2,3} (Error Collection initialize not permitted)
• The initializer expression must have a compile-time type which cannot be the null type. e.g var z = null; ( Error can’t null)
• The local variable declaration cannot include multiple declarators. var y = x => x + 1; // Error, lambda expressions do not have a type
• The initializer cannot refer to the declared variable itself e.g. var v = v++;
• Only local-variable-declaration, for-initializer, resource-acquisitio(Using Statement) and foreach-statement can contain implicitly typed local variable declarations.

Extension Methods:

• Used to Extend Existing types and constructed type with additional methods
• Recommended only where instance methods are not possible due to limited functionality
• Can only be declared in non-generic, non-nested static classes
• First parameter of an extension method can have no modifiers other than this, and the parameter type cannot be a pointer type.
• have all the capabilities of regular static methods
• Once imported, extension methods can be invoked using instance method syntax
• The preceding rules mean that instance methods take precedence over extension methods, and extension methods available in inner namespace declarations take precedence over extension methods available in outer namespace declarations

In C# 2.0 :

Only Method can be instance or static method

In C# 3.0 :

Declaration:

namespace Inder.Utilities
{
public static class Extensions
{

//Extension Method for String class
public static int ToInt32(this string s)
{
return Int32.Parse(s);
}

//Extension Method for Generic class

public static T[ ] Slice(this T[ ] source, int index, int count)
{
if (index < 0 count < 0 source.Length – index < count)
throw new ArgumentException();
T[ ] result = new T[count];
Array.Copy(source, index, result, 0, count);
return result;
}
}
}



How to Use:
string s = "1234";
int i = s.ToInt32(); // Same as Extensions.ToInt32(s)

int[] digits = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; //Same as Extensions.Slice(digits, 4, 3)

int[] a = digits.Slice(4, 3);

Precedence example

In the example, the B method takes precedence over the first extension method, and the C method takes precedence over both extension methods.

public static class E
{
public static void F(this object obj, int i) { }

public static void F(this object obj, string s) { }
}

class A { }

class B
{

public void F(int i)
{
}

}

class C
{

public void F(object obj)
{
}

}
class X
{

static void Test(A a, B b, C c)
{

a.F(1); // E.F(object, int)
a.F("hello"); // E.F(object, string)
b.F(1); // B.F(int)
b.F("hello"); // E.F(object, string)
c.F(1); // C.F(object)
c.F("hello"); // C.F(object)
}
}



Object and Collection Initializes:

It means we can initialize object properties while creating object.

Example:

public class Position
{
int x, y;
public int X { get { return x; } set { x = value; } }
public int Y { get { return y; } set { y = value; } }
}

In C# 2.0 :

Object Creation and Intialzation:

Position objPosition = new Position();
objPosition.X = 12;
objPosition.Y = 12;

Collection Creation and Intialzation:

List digits = new List;
digits.Add(1);
digits.Add(2);
digits.Add(3);
digits.Add(4);
digits.Add(5);
digits.Add(6);
digits.Add(7);
digits.Add(8);
digits.Add(9);



In C# 3.0 :

Object Creation and Intialzation:

Position objPosition = new Position{X= 12, Y=12};


Collection Creation and Intialzation:

List digits = new List { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };



Interface and Class

If a class implements two interfaces that contain a member with the same signature

http://msdn.microsoft.com/en-us/library/ms173157.aspx