Wednesday, May 21, 2008

I miss some interfaces

I don't think I'm the only one that misses some interfaces in the .NET framework. A while ago I worked with a graphics framework written in .NET and I wanted to create a generic class that would handle different types of data items collection. The type would be byte, int, double, Decimal and so on. The class should then be able to perform some calculations.

Now, the problem with generic classes is that it is generic. You should be able to put in any type and the class will work. But, one thing you can do is set some constraint on your generic class and only allow types implementing a specific interface or only value type/reference type. You do this by setting:

public class MyClass<T> where T : IMyInteface

Now the problem in my situation was that I wanted do perform calculations using standard value types. Ok, I could set that the generic type must only be a value type, but that wouldn't solve any of my problems. The real problem is that there is no interface for addition, subtraction... This means that you can not use generic classes and calculation :/. I had to limit my data type to double (or copy the class and implement the same class for byte, int).

My question is, will we see an interface for mathematical operations in the .NET framework?

Bilden “” kan inte visas, då den innehåller fel.

1 comment:

Cpt. Kirk said...

dont you know about abstract classes, extension methods and Lambda expressions ?

everything you ask for is possible :)