Default for generic type? [duplicate]

我与影子孤独终老i 提交于 2019-12-22 01:46:48

问题


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:


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.



来源:https://stackoverflow.com/questions/2747874/default-for-generic-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!