Skip to content

Irritation: Necessity of separate struct/class methods for dealing with nullable generics #7902

Discussion options

You must be logged in to vote

These limitations are inherent to .NET, not C#.

Part of the confusion comes from the C# syntax for nullable reference types, T?. In that case the actual type is just T as it is already nullable. But for value types, T? actually refers to the special wrapper type Nullable<T>. It's not possible for the signature to vary based on the generic type argument, so without the struct constraint the T? type has to refer to T instead. As it's not possible for int or other value types to be null, the closest you can get is default or 0.

The second issue is that the runtime does not include generic type parameter constraints as part of the method signature. So it's not possible for two methods with id…

Replies: 2 comments 4 replies

Comment options

You must be logged in to vote
0 replies
Answer selected by LyndonGingerich
Comment options

You must be logged in to vote
4 replies
@HaloFour
Comment options

@LyndonGingerich
Comment options

@KennethHoff
Comment options

@HaloFour
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants