class MyClass<T>
is a generic class. But what is class MyClass<T?>
?
I'm only guessing it defines T as nullable type ? But if its so I don't understand how and why.
Can't find anything about it on web.
Asked
Active
Viewed 639 times
-1

Riva
- 637
- 6
- 14
-
6Are you sure you're looking at C# code? That doesn't compile. – dcastro Dec 05 '13 at 14:10
-
4That's a syntax error – Alex Dec 05 '13 at 14:10
-
3its just a `Syntax Error`! – neshpro9 Dec 05 '13 at 14:12
-
1`class MyClass
` will always be a syntax error. You could have a `?` in a generic type, but not in its definition. This is completely legal: `List – Luaan Dec 05 '13 at 14:19Test () where T : struct { return new List (); }` -
I have encountered it in 'Extended WPF Toolkit'. Specifically in NumericUpDown class. Since the lib is written in c# and it does compile (I tried) I thought its some syntax I didn't know. -- Maybe it's something custom defined by the authors ? – Riva Dec 05 '13 at 14:30
-
@Vlad Lazarenko - the duplicate you sugested is wrong. I am not asking about nullable types. That is something very different then generics. – Riva Dec 05 '13 at 14:33
-
2No, you've misread. There is no `class MyClass
`. There's `class MyClass – Luaan Dec 05 '13 at 14:33: OtherClass where T : struct`, which is perfectly legal. In other words, you can make a `class NullableList : List where T : struct` which is a list that only takes nullables. -
@Luaan - Yes, you are right. Thanks for explanation -- Can you make it an answer ? – Riva Dec 05 '13 at 14:39
1 Answers
2
No, you've misread. There is no class MyClass<T?>
.
There's class MyClass<T> : OtherClass<T?> where T : struct
, which is perfectly legal.
In other words, you can do this:
class NullableList<T> : List<T?> where T : struct {}
And you'll get a generic list, which uses a nullable type as its generic type, eg. if you create a NullableList<int>
, it will have a method Add(int? val)
.
Funnily enough, through the syntactic sugar magic of nullables, you can't have a Nullable of a Nullable (eg. Nullable<Nullable<int>>
for example), even though Nullable<T>
is a struct. It produces a compile-time error.

Luaan
- 62,244
- 7
- 97
- 116