Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Less defined generics in c#?

Is there a way to use a collection of a generic class, without supplying the underlying type ? Let's explain :

Here is what I'd like to have :

class TimeSerie<TValue> {
enter code here
}

List<TimeSerie<?>> blah;

Here is what I have to do so far :

class TimeSerie {}
class TypedTimeSerie<TValue> : TimeSerie {}

List<TimeSerie> blah;

So, any way to use the nice first solution ? (although I guess it would raise problems when trying to cast, for a loop for example ...)

like image 586
Wam Avatar asked Dec 07 '25 10:12

Wam


1 Answers

You can make your using code generic too... but at some point you do have to specify the type argument. You're not going to be able to create an instance of the generic type without the type argument being known. You can provide that information at execution time using reflection if you must, but it has to be there somehow.

like image 122
Jon Skeet Avatar answered Dec 11 '25 14:12

Jon Skeet