Restricting Generic type parameter T to int, uint, short

  • Thread starter Thread starter KK
  • Start date Start date
K

KK

Dear All

I'm planning to develop a class for my project which can operate on only
few known data types

For example :

public class MyOperations<T>
{
public T[] ChangeBounds()
{
}
}
Here possible types for T are int, uint, short, float.
How can restrict T to only these types?
Thanks in advance for your help.
 
My two ideas.

Create a new type like MyLimitedNumbers that can hold only int, uint, short
and float, so that you can use that as where constraint.

public class MyOperations<T> where T:MyLimitedNumbers

Otherwise throw an exception from the constructor if the type is not valid.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top