Skip to content
Discussion options

You must be logged in to vote

Why is that? Shouldn't the compiler be aware that assigning a Guid or null to a Guid? are valid operations?

No it does not. For one thing, Guid and Guid? are completely different types, and type arguments on classes and structs must be exact because they can't be co-/contravariant. Even when using reference types, you can't convert a Task<string> to a Task<object>, and you can't convert a Task<Guid> to a Task<Guid?> for the same reason.
On the other hand, the statement Guid? emtpyId = Guid.Empty; simply uses the implicit conversions defined on Nullable<T> (which can't be used for inferring type arguments) and so doesn't produce the same error.

Note, by the way, that a similar restrictio…

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@marwalsch
Comment options

Answer selected by marwalsch
Comment options

You must be logged in to vote
1 reply
@marwalsch
Comment options

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