Wednesday, July 06, 2005

The C# Whidbey

C# has always had a reputation as a clean language with lots of innovation.The Whidbey-release of Visual Studio .NET ships with a new version of the C# compiler that has a number of great new features. Some of them will be implemented as generic runtime features that will show up in other languages as well, and some are truly C#-specific.

Anonymous Methods

If you have developed in C#, you are probably familiar with Delegates.Delegates are objects that encapsulate references to functions. One of the most common uses of Delegates is the implementation of event handler code.Here is an Exampleprivate void button1_Click( object sender, System.EventArgs e){ MessageBox.Show("Hello");}
Actually in the form designer code we can see,
this.button1.Click += new System.EventHandler( this.button1_Click);
The event handler method has to conform to a certain signature (two parameters of type object and System.EventArgs in this case). This is defined in the System.EventHandler delegate.
Rather than instantiating a delegate and creating a method to point to, thecode for the whole handler method can be created inline. No method name is defined (hence the name Anonymous Methods), but parameters are still required.Here's what that syntax looks like:
this.button1.Click += delegate(object sender, EventArgs e) { MessageBox.Show("Test"); };
As you can see, what is assigned to the Click event is not a pointer to a method, but the entire method code itself including the parameters, and without a method-name. Note that there has to be a semi-colon at the end, because all of this is really just one line of code.

Partial Classes

In general, classes are defined in a single source file. Sometimes, this can lead to difficulties.
The new version of C# introduces a feature that can solve these problems: partial classes. These are classes that are defined in more than one source file. This way, part of a partial class could be maintained by the code generator and a second part of the class could be maintained by the developer. When the code gets compiled, the C# compiler performs what can be described as a fancy text merge operation that combines all parts into one class.
Here's a code sample. First, there could be one part of a class defined in one source file:
public partial class Class1{ public string Test() { return "Test"; }}
Then, there could be a second source file with the following class definition:
public partial class Class1{ public string SomethingElse() { return "Something Else"; }}
The significant part is the definition of the class. First of all, both class definitions sport the same class name. Secondly, there is the partial keyword, which makes the duplicate class name a valid construct, and instructs the compiler to merge the two parts together.

Generics

Creating specific collection classes for every possible type that occurs in the .NET Framework, as well as your own code, is not feasible either. You'd be doubling the number of used classes on the spot!Generics solve these issues. They allow creating code that more generic, but when the code is used, a specific type is assumed.
public class MyCollection{ public void Add(myType o1) { // ... }
public myType GetByIndex(int i) { // ... }}
In this example, the whole class is defined with a parameter called myType. This myType parameter (there could be more than one) is used to define the parameter type as well as the return type. Of course myType is not defined anywhere. Instead, the code that uses this class defines the "real" type that is to be used. For instance, if you want to use this collection to store integers, you use it like so:
MyCollection Col1 = new MyCollection();Col1.Add(1);
Internally, the collection now operates with integers. There is no boxing and unboxing. Also, values retrieved from the collection are integers without having to be converted. And on top of that, this instance of the collection is limited to storing integers. An attempt to assign a DataSet is trapped as an error during compilation. You could easily use the whole collection for DataSets as well:
MyCollection Col1 = new MyCollection();

0 Comments:

Post a Comment

<< Home