Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default for generic type? [duplicate]

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.

like image 548
mpen Avatar asked Apr 30 '10 21:04

mpen


People also ask

What is the default generic type in C#?

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

What is generic data type?

Definition: “A generic type is a generic class or interface that is parameterized over types.” Essentially, generic types allow you to write a general, generic class (or method) that works with different types, allowing for code re-use.

How do you find the type of generic type?

Use the IsGenericType property to determine whether the type is generic, and use the IsGenericTypeDefinition property to determine whether the type is a generic type definition. Get an array that contains the generic type arguments, using the GetGenericArguments method.

Can a generic type be null?

This means that you can put any object in a collection because all classes in the C# programming language extend from the object base class. Also, we cannot simply return null from a generic method like in normal method.


1 Answers

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.

like image 127
Reed Copsey Avatar answered Oct 06 '22 01:10

Reed Copsey