Generic method where T is type1 or type2

后端 未结 7 1854
陌清茗
陌清茗 2021-02-12 01:58

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

example:

public void Foo(T number)
{
}         


        
相关标签:
7条回答
  • 2021-02-12 02:07

    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, ...).

    0 讨论(0)
  • 2021-02-12 02:11

    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.

    0 讨论(0)
  • 2021-02-12 02:17

    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

    0 讨论(0)
  • 2021-02-12 02:20

    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
    }
    
    0 讨论(0)
  • 2021-02-12 02:21

    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.

    0 讨论(0)
  • 2021-02-12 02:21

    I don't think this is currently possible.

    This question about creating a math library sort of covers the same ground, and includes some work arounds.

    0 讨论(0)
提交回复
热议问题