Wednesday, June 23, 2010

C# 3.0 Features

1. Implicitly Typed Variable (var)
Implicitly typed local variable is a variable that can be declared without specifying the .NET type explicitly. The type of that variable will be decided by the complier from the expression on the right side. Here’s an example of implicitly typed variable (var)
var i = 5;
var y = "dotnet";
var it = new int[] { 1, 2, 3 };
The above mentioned C# 3.0 syntax will be equivalent to the below mentioned C# 2.0 syntax.
int i = 5;
string y = “dotnet”;

Limitations
• variable must be initialized to some expression that can not be null
var x;
• variable can not be assigned to null
var x = null;
• The initializer must be an expression. You can’t initialize it with an array.
var r = { 1, 2, 3 };
• The implicitly-type local variable can’t be initialized with different types more than one time.
var t = 1;
t = "dotnet"; // ERROR
2. Object Initializers
To get started, let’s look at the standard way of initializing an object with data in C# 2.0 using constructors.
Employee e = new Employee("Harry", "Patel", "586-879-2563");
In C# 3.0, you can easily assign data to specific properties in a type with having to create an explicit constructor. Here’s an example of using an object initializer to assign property data to an employee type.
Employee e = new Employee() {FirstName="Harry", LastName="Patel", Phone="586-879-2563", City="NewYork"};
You can even use object initializers to create the sub object.
Employee e = new Employee()
{
FirstName = "Harry",
LastName = "Patel",
Address = new Address()
{
Street = "256, xyz",
City = "NewYork"
}
};
You can create collection initializer also. Here’s an example of collection initializer.
List square = new List()
{
new Point(){x=10},
new Point(){X=10}
};
3. Anonymous Types
Types those are not having any names are Anonymous types. Here’s an example of Anonymous types.
var cust = new { custID = 10, custName = "Harry" };
var cust1 = new { custID1 = "Harry", custName1 = 20 };
In above example, custID and custName declared with no specific data types.
4. Extension Methods
If you want to have your own custom method in string class then you can do that using extension methods.Here’s an example of extension methods.
double d = 12.25263;
// Calling the Extension method here.
double newD = d.RoundMe();
Console.WriteLine(newD);
Static Class implemented Extension Methods
Extension method must have static method and class having it must be a static class.
public static class stringExtensionClass
{
// Add "this" keyword to make it extension method enabled.
public static double RoundMe(this double i)
{
return Math.Round(i, 0);
}
}
Limitations
• Extension methods cannot be used to override existing methods
• An extension method with the same name and signature as an instance method will not be called
• The concept of extension methods cannot be applied to fields, properties or events
5. Lambda Expressions
Lambda Expression is another way to writing an Anonymous method(C# 2.0). Consider the following C# example:
delegate bool Delegate1(int i);
public void Method1()
{
Delegate1 d = new Delegate1(Method2);
// other code here
}
private bool Method2(int i)
{
return i > 2;
}
Simplified above code using C# 2.0 Anonymous methods.
delegate bool Delegate1(int i);
private void Method1()
{
Delegate1 d = delegate(int i){return i > 2;};
// other code here
}
Lambda expressions take this to the next level of conciseness. Here’s the above code re-written using lambda expressions.
private void Method1()
{
// line using C# 2.0 Anonymous types.
Delegate1 d = delegate(int i){return i > 2;};
// line using C# 3.0 Lambda Expressions.
Delegate1 d = i=> i > 2;
}
6. Auto Implemented Property
We are used to creating property field using lots of statements. Auto implemented property is a solution:
Property declaration before .net fx 3.0
private int x1;
public int X
{
get { return x1; }
set { x1 = value; }
}
C# 3.0 auto implemented property replaces above code with following line.
public int x { get; set; }
For creating read-only property just prefix set tag with private access modifier.
public int ReadOnlyProp { get; private set; }
Limitations
• Both Get and Set properties are required to create property.
public int ReadOnlyProp { get; set; } // ERROR
• Can not assign private to both Get and Set property.
public int ReadOnlyProp { private get; private set; } // ERROR
• Get and Set method’s access modifiers must be more restrictive than Property.
private int ReadOnlyProp {public get; set; } // ERROR
That’s All for C# 3.0…
Happy dotnetting… :)

0 comments:

Post a Comment