【发布时间】:2013-05-14 11:27:55
【问题描述】:
如何创建accepts only a type of Integer, Long and String. 的泛型类型
我知道我们可以限制单个类的类型或通过使用以下代码实现接口
public class MyGenericClass<T> where T:Integer{ }
或处理 int,long 但不处理字符串
public class MyGenericClass<T> where T:struct
是否可以创建一个只接受 Integer、Long 和 String 类型的泛型?
【问题讨论】:
-
你确定泛型是你想要的吗?泛型通常用于全面使用。你到底想解决什么问题?
-
@Moo-Juice 这是面试官问的。我问需要,他回复
that is requirement:( -
我的回答是“你不能,如果可以的话,它也不会是适当的泛型。改为编写三个单独的类。”
标签: c# generics collections