-
Switch expression returns a null or Enum default value for nullable Enum.OS: windows 10 and Linux Mint 20 Cinnamon - kernel 5.4.0-48-generic GeneralIn example 1 the value of orientation is Orientation.North
In example 2 the value of orientation is null
Unit test: |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
I believe this is the expected behavior. The The best common type of The best common type of In these cases, where it's not clear what |
Beta Was this translation helpful? Give feedback.
-
@jaredpar can you pleae either close, or move to csharp-lang? I don't have rights to do so. |
Beta Was this translation helpful? Give feedback.
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
anddefault
isOrientation
, sodefault
becomesdefault(Orientation)
.The best common type of
default
doesn't exist, so target-typing is used anddefault
becomesdefault(Orientation?)
.In these cases, where it's not clear what
default
means, I'd suggest being more explicit and writing something likeOrientation.North
or0
, ornull
.