Skip to content
Discussion options

You must be logged in to vote

if I did Foo<int> would T be int? or int?

It would be int.

And I did Foo<int?> is T int?? or int??

There's no such thing as int??. Using T? requires you to add a generic parameter type constraint where T : struct to your method, because Nullable<T> likewise constraints T to be struct and that constraint is "propagated" to your method. It appears that a where T : struct explicitly disallows T to be a nullable value type (my guess is to prevent nested or multiply-nullable types), so once you add the constraint, you'll no longer be able to use e.g. int? as a generic type argument.

Replies: 4 comments

Comment options

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

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants
Converted from issue

This discussion was converted from issue #1611 on October 16, 2020 04:30.