Generic method where T is type1 or type2

后端 未结 7 1855
陌清茗
陌清茗 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, ...).

提交回复
热议问题