|
-
December 19th, 2007, 04:47 PM
#1
Generics for numbers?
I'm attempting to write an application which does quite a bit of math, and since speed is a real factor, I wanted to provide the end-user with a choice on how much precision they want (as a speed trade-off). So, I came up with a vector class like this:
Code:
public partial class Vector<T> where T : float, double, decimal
{ /* stuff */ }
However, it gives me a strange set of errors:
Code:
Error 1 'float' is not a valid constraint. A type used as a constraint must be an interface, a non-sealed class or a type parameter. D:\Coding\C# Projects\Work\MyApp\Math\VectorMath.cs 23 19 MyApp
Error 2 'double' is not a valid constraint. A type used as a constraint must be an interface, a non-sealed class or a type parameter. D:\Coding\C# Projects\Work\MyApp\Math\VectorMath.cs 23 26 MyApp
Error 3 'decimal' is not a valid constraint. A type used as a constraint must be an interface, a non-sealed class or a type parameter. D:\Coding\C# Projects\Work\MyApp\Math\VectorMath.cs 23 34 MyApp
I understand what it means, but I don't understand why there would be such a restriction on type constraints. Is there another way I should be doing this, short of three separate classes? I don't want to implement the entire program three times, and there are many numeric operations I'd need to do that couldn't be done on just a general object.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|