Saturday, January 07, 2006

Generic Wishlist for new version of C#

IMHO, Generics are one of the best parts of C# v2.0. Lots of redundant brain dead code can be eliminated by making generic version.

Generic constraints are also a beautiful invention but they fall painfully short in one area, the new constraint. The new constraint is limited in that you can only specify that a type has an empty accessible constructor. My want is that you can specify the types that are a part of that constructor like so.


public class Foo
{
public static T CreationHelper(int value)
where T : class, new(int)
{
// Some setup code followed by
return new T(int);
}
}

The key is the code that could go before and after the new. Logging code can be inserted or if you're dealing with memory, management code can be inserted. I tend to encounter the want for this feature when I am doing interop code.

0 Comments:

Post a Comment

<< Home