Home All Groups Group Topic Archive Search About
Author
4 Mar 2006 1:16 PM
Ruin
Hello,
i can use this chunk of code to constraint type T only to value types:
public class Test<T> where T : struct {...}
Is there a possibility to constraint type T only to number types (int,
float, decimal, etc.)?
Thanks

Author
4 Mar 2006 1:52 PM
Jon Skeet [C# MVP]
Ruin <R***@discussions.microsoft.com> wrote:
> i can use this chunk of code to constraint type T only to value types:
> public class Test<T> where T : struct {...}
> Is there a possibility to constraint type T only to number types (int,
> float, decimal, etc.)?

No - there's nothing which particularly denotes numeric types.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Author
4 Mar 2006 3:27 PM
Michael D. Ober
Unfortunately dotNet doesn't have CLU's constraining clauses which require
the allowed T classes have certain methods or properties.

You could mimic this at runtime in the constructor by using reflection, but
it's not a compile time feature.

Mike Ober.

Show quote
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MPG.1e73abebf13038b798ced1@msnews.microsoft.com...
> Ruin <R***@discussions.microsoft.com> wrote:
> > i can use this chunk of code to constraint type T only to value types:
> > public class Test<T> where T : struct {...}
> > Is there a possibility to constraint type T only to number types (int,
> > float, decimal, etc.)?
>
> No - there's nothing which particularly denotes numeric types.
>
> --
> Jon Skeet - <sk***@pobox.com>
> http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
> If replying to the group, please do not mail me too
>
Author
4 Mar 2006 5:44 PM
Nick Hounsome
"Ruin" <R***@discussions.microsoft.com> wrote in message
news:07FEBFE3-3AF2-4CB6-90A7-E20018DE81C6@microsoft.com...
> Hello,
> i can use this chunk of code to constraint type T only to value types:
> public class Test<T> where T : struct {...}
> Is there a possibility to constraint type T only to number types (int,
> float, decimal, etc.)?
> Thanks

No.

(You will have to use method overloading to do any sort of "generic"
computation)

AddThis Social Bookmark Button