Nullable-generics interaction doesn't work as expected. #7609
-
I posted this question recently about how generic return types and the handling of nullable types is handled and how it seems to be not correct. The TL;DR is this: if I have a method The bug (is it a bug or expected?) lies the case where it's a nullable generic parameter that is unconstrained to be either a reference type (i.e. Are there plans to address this? This behavior seems to be incorrect since the actual type of the expression |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
This is by design. When T is unconstrained (no If you have the
No. This has been how generics have worked in .net and c# for 20 years. Far too much code has been written that depends on things working in this exact fashion. |
Beta Was this translation helpful? Give feedback.
This is by design. When T is unconstrained (no
struct
orclass
constraint) thenT?
simply means"can have its default value"
(notcan be null
).If you have the
class
constraint then it is known to be a reference type, and can benull
. Similarly, if you have thestruct
constraint thenT?
meansNullable<T>
which can also be null.No. This has been how generics have worked in .net and c# for 20 years. Far too much code has been written that depends on things working in this exact fashion.