Default for generic type?

C#Generics.Net 4.0

C# Problem Overview


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.

C# Solutions


Solution 1 - C#

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmpenView Question on Stackoverflow
Solution 1 - C#Reed CopseyView Answer on Stackoverflow