Four New Features of C# 2.0
Posted in C# by Dr Christian Bridge-Harrington
C# was first released around 2000 and was followed by C# 2.0 with the release of Visual Studio 2005 and the .NET 2.0 Framework. Version 2.0 built upon the first release and includes four major new enhancements. This article describes these four new features and how they will improve our developments.
Anonymous Methods
Anonymous methods are used to attach event handlers quickly and simply without having to explicitly declare a new method. You create the event handler in the same method where you declare the event, and let the compiler worry about separating them out. For example, previously you would declare the event and handler like this:
public Form1()
{
button1.Click += new EventHandler(Button1_Click);
}
private void Button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Button clicked");
}
In C# 2.0, however, you can delcare the event handler as follows:
public Form1()
{
button1.Click=delegate(object sender, EventArgs e)
{
MessageBox.Show("Button clicked");
}
}
This can simplify your code and lead to better "readability", particularly with the handling of asynchronous events where you can define how to handle the completion of the asynchronous task, in the same context where you start the task.
Additionally, inside the anonymous method you can access any variables declared in the method in which it is defined. You can also declare a delegate to point to the anonymous method, and this allows you to attach the anonymous method to several different events.
public Form1()
{
EventHandler buttonHandler =
delegate(object sender, EventArgs e)
{
MessageBox.Show("Button clicked");
}
button1.Click += buttonHandler;
button2.Click += buttonHandler;
}
Iterators
The beauty of using enumeration, and iterators, is the ease with which you can retrieve all the required values using a simple statement such as:
foreach(OrderItem item in orderCollection)
{
//do something with the item
}
In C# 1.0, creating a class that supported enumeration was somewhat painful. However, C# 2.0 has solved this problem with the ability to create a single public method named GetEnumerator() that returns all the required information using the yield return keyword.
For example, the orderCollection class above contains a collection or order objects. To iterate this collection of order, we implement the following method in the orderCollection class:
public IEnumerator<OrderItem> GetEnumerator()
{
foreach(OrderItem item in orderItems)
{
yield return item;
}
}
Generics
Generics provide a new type-safe means of manipulating objects. Previously using collections such as the ArrayList meant that everything inserted into the list was treated as an Object. This provides no guarantee that mistakes could not be made and different types of object be inserted into the list. This can later lead to problems when you thought you were retrieving an object of type A, when in fact it was of type B. This may not always causes exceptions, and more subtle errors could be propagated through the code.
C# 2.0 solves this, and ther related issues, by allowing you to specify exactly what type of object is allowed into the collection. This is done at declaration of the collection, for example, to declare an ArrayList that can contain only String objects:
ArrayList<String> newList = new ArrayList<String>();
The new generics feature is vast and Microsoft provides much documentation on the use, features and wrinkles in generics. For example, you can constrain collection to objects that implement a certain type of interface.
Partial Classes
Partial classes give you the ability to split your code into more than one file. A partial class is declared with the keyword partial, for example:
public partial class OrderHalf {
public void Method()
{}
}
public partial class OrderHalf
{
public void AnotherMethod()
{}
}
Note, that although the classes are named the same, they would have to be saved to different file names.
The uses for partial classes is not immediately obvious. One use would be to split large classes up into more managable chunks, however if you need to do that you have probably not designed your classes very well to start with. A better use is to hide automatically generated code, and this is what Visual Studio does with partial classes. When you create a new form the designer creates one file for the code that generates the form, and one for you to implement your methods. This provides for a neater programming environment.
For more details on the C# 2.0 language, see the C# Language Page at http://msdn.microsoft.com/vcsharp/team/language
|