Default for generic type? [duplicate]

2020-08-09 07:29发布

Is it possible to do something like

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

(note the =int) ?

Before you suggest it, yes, I know I can just add another line:

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

But I'm wondering if it's possible to do it as a param.

1条回答
Viruses.
2楼-- · 2020-08-09 07:49

No. There is no option for default types on generic types in C#.

Your second example is often the "best" option available, if you need this behavior.

查看更多
登录 后发表回答