Sunday, April 13, 2008

.NET 2.0

What are the new features of .NET 2.0?

Generics, anonymous methods, partial classes, iterators, property visibility (separate visibility for get and set) and static classes. See for more information about these features.

What are the new 2.0 features useful for?

Generics are useful for writing efficient type-independent code, particularly where the types might include value types. The obvious application is container classes, and the .NET 2.0 class library includes a suite of generic container classes in the System.Collections.Generic namespace. Here's a simple example of a generic container class being used:

    List myList = new List();
myList.Add( 10 );

Anonymous methods reduce the amount of code you have to write when using delegates, and are therefore especially useful for GUI programming. Here's an example

    AppDomain.CurrentDomain.ProcessExit += delegate { Console.WriteLine("Process ending ..."); };

Partial classes is a useful feature for separating machine-generated code from hand-written code in the same class, and will therefore be heavily used by development tools such as Visual Studio.

Iterators reduce the amount of code you need to write to implement IEnumerable/IEnumerator. Here's some sample code:

    static void Main()
RandomEnumerator re = new RandomEnumerator( 5 );
foreach( double r in re )
Console.WriteLine( r );

class RandomEnumerator : IEnumerable
public RandomEnumerator(int size) { m_size = size; }

public IEnumerator GetEnumerator()
Random rand = new Random();
for( int i=0; i <>
yield return rand.NextDouble();

int m_size = 0;

The use of 'yield return' is rather strange at first sight. It effectively synthethises an implementation of IEnumerator, something we had to do manually in .NET 1.x.

What's the problem with .NET generics?

.NET generics work great for container classes. But what about other uses? Well, it turns out that .NET generics have a major limitation - they require the type parameter to be constrained. For example, you cannot do this:

    static class Disposer
public static void Dispose(T obj) { obj.Dispose(); }

The C# compiler will refuse to compile this code, as the type T has not been constrained, and therefore only supports the methods of System.Object. Dispose is not a method on System.Object, so the compilation fails. To fix this code, we need to add a where clause, to reassure the compiler that our type T does indeed have a Dispose method

    static class Disposer where T : IDisposable
public static void Dispose(T obj) { obj.Dispose(); }

The problem is that the requirement for explicit contraints is very limiting. We can use constraints to say that T implements a particular interface, but we can't dilute that to simply say that T implements a particular method. Contrast this with C++ templates (for example), where no constraint at all is required - it is assumed (and verified at compile time) that if the code invokes the Dispose() method on a type, then the type will support the method.

In fact, after writing generic code with interface constraints, we quickly see that we haven't gained much over non-generic interface-based programming. For example, we can easily rewrite the Disposer class without generics:

    static class Disposer
public static void Dispose( IDisposable obj ) { obj.Dispose(); }

For more on this topic, start by reading the following articles:

Bruce Eckel:
Ian Griffiths:
Charles Cook:

What's new in the .NET 2.0 class library?

Here is a selection of new features in the .NET 2.0 class library:

  • Generic collections in the System.Collections.Generic namespace.
  • The System.Nullable type. (Note that C# has special syntax for this type, e.g. int? is equivalent to Nullable)
  • The GZipStream and DeflateStream classes in the System.IO.Compression namespace.
  • The Semaphore class in the System.Threading namespace.
  • Wrappers for DPAPI in the form of the ProtectedData and ProtectedMemory classes in the System.Security.Cryptography namespace.
  • The IPC remoting channel in the System.Runtime.Remoting.Channels.Ipc namespace, for optimised intra-machine communication.

and many, many more. See,VS.80).aspx for a comprehensive list of changes.

No comments:

Post a Comment