public enum Test { Yes, No }
I have these two switch expressions. The one below gives a CS8509 warning:
Test? test = null;
var result = test switch
{
null => "Null",
Test.Yes => "Yes",
Test.No => "No",
};
But moving the null case to the end resolves it. Does anybody know why this happens? This has to be a bug, correct?
Test? test = null;
var result = test switch
{
Test.Yes => "Yes",
Test.No => "No",
null => "Null",
};
Project settings:
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<LangVersion>10.0</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<NoWarn>8524</NoWarn>
</PropertyGroup>
Found this issue at the github with very similar problem. The resolution is:
You have disabled one of the (at least) two warnings possible here, so you get this inconsistent behaviour. The same can be reproduced the other way around:
Demo @sharplab