泛型类型的默认值?

时间:2010-04-30 21:39:30

标签: c# generics .net-4.0

是否可以执行类似

的操作
public class PriorityQueue<TValue, TPriority=int> where TPriority : IComparable

(请注意=int)?

在你建议之前,是的,我知道我可以添加另一行:

public class PriorityQueue<TValue> : PriorityQueue<TValue, int> { }

但我想知道是否有可能将其作为一个参数。

1 个答案:

答案 0 :(得分:31)

没有。 C#中的泛型类型没有默认类型选项。

如果您需要此行为,您的第二个示例通常是“最佳”选项。