|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Class constraintsHello,
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 Ruin <R***@discussions.microsoft.com> wrote:
> i can use this chunk of code to constraint type T only to value types: No - there's nothing which particularly denotes numeric types.> public class Test<T> where T : struct {...} > Is there a possibility to constraint type T only to number types (int, > float, decimal, etc.)? -- 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 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 > "Ruin" <R***@discussions.microsoft.com> wrote in message No.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 (You will have to use method overloading to do any sort of "generic" computation) |
|||||||||||||||||||||||