Skip to content

Switch expression returns null or the default value (0) for nullable Enum. #3958

Answered by svick
djs2014 asked this question in Q&A
Discussion options

You must be logged in to vote

I believe this is the expected behavior. The switch expression is target-typed, but only when there isn't a best common type for the right-hand side expressions.

The best common type of Orientation.East and default is Orientation, so default becomes default(Orientation).

The best common type of default doesn't exist, so target-typing is used and default becomes default(Orientation?).

In these cases, where it's not clear what default means, I'd suggest being more explicit and writing something like Orientation.North or 0, or null.

Replies: 3 comments 3 replies

Comment options

You must be logged in to vote
3 replies
@333fred
Comment options

@djs2014
Comment options

@jnm2
Comment options

jnm2 Oct 1, 2020
Collaborator

Answer selected by 333fred
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
6 participants
Converted from issue

This discussion was converted from issue #3958 on September 30, 2020 22:48.