Generic method where T is type1 or type2

43,575

Solution 1

Although you could use a generic constraint to limit the type of each generic argument T, unfortunately there is none that would allow you to enforce at compile time whether T is type1 or type2.

Nor is there any way to enforce at compile time that your generic argument can only be of any primitive type (int, long, double, ...).

Solution 2

For ReferenceType objects you can do

public void DoIt<T>(T someParameter) where T : IMyType
{

}

...

public interface IMyType
{
}

public class Type1 : IMyType
{
}

public class Type2 : IMyType
{
}

For your case using long as parameter will constrain usage to longs and ints anyway.

public void DoIt(long someParameter)
{

}

to constrain to any value types (like: int, double, short, decimal) you can use:

public void DoIt<T>(T someParameter) where T : struct
{

}

for more information you can check official documentation here

Solution 3

No.

That doesn't make sense; T would not have any usable compile-time type in the method.

Instead, you should make two overloaded methods.

Solution 4

Use overloaded methods instead:

public void Foo(int number)
{
} 

public void Foo(long number)
{
}

You cannot perform arithmetical operations on generic types anyway. Note that you can pass an int value to a long parameter. It will automatically be converted to long. Having just a single method with a long parameter could therefore be sufficient.

Older programming languages worked after the principle "There can be only one". C# allows you to have several methods with the same name in the same class, interface or struct. These methods must have a different signature. This means, that they must have a different number of parameters or parameters with different types (or both). This is called method overloading.

Solution 5

I know this is an old question, and this doesn't perfectly answer it, but you can do this with a single method, rather than creating multiples, or using generic constraints... especially useful if you have 20 odd types to check.

Obviously you don't get the compiler type checking as you do when using a constraint, but this can help in certain circumstances...

public void MyMethod<T>()
{
    if (!typeof(T).Equals(typeof(int)) &&
        !typeof(T).Equals(typeof(long)))
            throw new Exception("T must be int or long");

    //your logic here
}
Share:
43,575
gdoron is supporting Monica
Author by

gdoron is supporting Monica

Doron Grinzaig

Updated on August 30, 2020

Comments

  • gdoron is supporting Monica
    gdoron is supporting Monica over 3 years

    Is there a way to declare a generic function that the generic type is of type1 or type2?

    example:

    public void Foo<T>(T number)
    {
    }
    

    Can I constraint T to be int or long