tags:

views:

48

answers:

1

Is there a way to constraint a generic type to only integral type?

In example, if I have a method

T[] sort<T>(T[] data) where : T ... {}

what would I put as constraint if I wanted to ensure the parameter will be some sort of integral type?

I have tryed looking at MSDN however it does not seem to mention anything about constraining and primitive types.

Thank you for your help.

+1  A: 

Nope. I asked this question before with VB.Net.

http://stackoverflow.com/questions/3358416/generic-classes-t-specifying-from-a-range-of-types-vb-net

As quoted from the page above:
Also, you mentioned constraining to numerics, but I don't think you can constrain to just that. You can however constrain to just primitive types (on the stack) and not allow objects (on the heap) by doing this: Public Structure Statistic(Of t As {Structure, IComparable}).

Jeffrey Kern