New in C-Sharp 3.0

Visit: https://zonixsoft.com

New in C-Sharp 3.0

New in C# 3.0
This article discusses the following major new enhancements expected in C# 3.0:
• Implicitly typed local variables
• Anonymous types
• Extension methods
• Object and collection initializers
• Lambda expressions
• Query expressions
• Expression Trees

Implicitly typed local variables:
C# 3.0 introduces a new keyword called “var”. Var allows you to declare a new variable, whose type is implicitly inferred from the expression used to initialize the variable.
Syntax: var i=1;
The preceding line initializes the variable i to value 1 and gives it the type of integer. It is not an object or variant.
Anonymous types:
C# 3.0 gives you the flexibility to create an instance of a class without having to write code for the class beforehand. So, you now can write code as shown below:
new {StudentID=1, StudentName=”XYZ”, Marks=90}
The preceding line of code, with the help of the “new” keyword, gives you a new type that has three properties: StudentID, StudentName, and Marks. Behind the scenes, the C# compiler would create a class that looks as follows:
class __Anonymous1
{
private int _StudentID = 1;
private string _StudentName = “XYZ”;
private int _Marks = 64;
public int StudentID {get { return _StudentID; } set { _StudentID = value; }}
public string StudentName {get { return _StudentName; } set { _StudentName = value; }}
public int Marks {get { return _Marks; } set { _Marks = value; }}
}

Extension methods:

Extension methods enable you to extend various types with additional static
methods. Extension methods can be declared only in static classes and are identified by
the keyword “this” as a modifier on the first parameter of the method.

The following is an example of a valid extension method:
public static int ToInt32(this string s)
{
return Convert.ToInt32(s) ;
}
Object and collection initializers:
C# 3.0 is expected to allow you to include an initializer that specifies the initial values of the members of a newly created object or collection. This enables you to combine declaration and initialization in one step.
For instance, if you defined a CoOrdinate class as follows:
public class CoOrdinate
{
public int x;
public int y;
}

You then could declare and initialize a CoOrdinate object using an object initializer, like this:

var myCoOrd = new CoOrdinate{ x = 0, y= 0} ;
You should easily be able to give values to collections in a rather concise and compact manner in C# 3.0.
In C# 2.0 Code we write :

List<string> animals = new List<string>();

animals.Add(“monkey”);
animals.Add(“donkey”);
animals.Add(“cow”);
animals.Add(“dog”);
animals.Add(“cat”);

It can be written in  C# 3.0 shortened to simply.
List<string> animals = new List<string> {“monkey”, “donkey”, “cow”, “dog”, “cat” } ;
Lambda expressions:
C#(1.x) allowed you to write code blocks in methods, which you could invoke easily using delegates.Delegates are definitely useful, and they are used throughout the framework, but in many instances you had to declare a method or a class just to use one. Thus, to give you an easier and more concise way of writing code, C# 2.0 allowed you to replace standard calls to delegates with anonymous methods.
In C# 2.0, using anonymous methods, you could rewrite the code as follows:
class Program
{
delegate void DemoDelegate();
static void Main(string[] args)
{
DemoDelegate myDelegate = delegate()
{
Console.Writeline(“Hiya!!”);
};
myDelegate();
}
}
The above code can now be replaced with the following code in C# 3.0:
class Program
{
delegate void DemoDelegate();
static void Main(string[] args)
{
DemoDelegate myDelegate = () => Console.WriteLine(“Hiya!!”) ;
myDelegate();
}
}

Reference Site: http://www.developer.com/net/csharp/article.php/10918_3561756_1


Bookmark and Share