I'm seeing some strange behavior when using a nullable long switch statement in VS2015 Update 1 that I'm not seeing in other Visual Studio releases where it runs as expected.
class Program
{
static void Main(string[] args)
{
NullableTest(-1);
NullableTest(0);
NullableTest(1);
NullableTest(2);
NullableTest(null);
}
public static void NullableTest(long? input)
{
string switch1;
switch (input)
{
case 0:
switch1 = "0";
break;
case 1:
switch1 = "1";
break;
default:
switch1 = "d";
break;
}
string switch2;
switch (input)
{
case -1:
switch2 = "-1";
break;
case 0:
switch2 = "0";
break;
case 1:
switch2 = "1";
break;
default:
switch2 = "d";
break;
}
string ifElse;
if (input == 0)
{
ifElse = "0";
}
else if (input == 1)
{
ifElse = "1";
}
else
{
ifElse = "d";
}
Console.WriteLine("Input = {0}, Switch 1 output = {1}, Switch 2 output = {2}, If Else = {3}", input, switch1, switch2, ifElse);
}
This sample code produces the following output (Aligned for readability):
Input = -1, Switch 1 output = d, Switch 2 output = d, If Else = d
Input = 0, Switch 1 output = 0, Switch 2 output = d, If Else = 0
Input = 1, Switch 1 output = d, Switch 2 output = d, If Else = 1
Input = 2, Switch 1 output = d, Switch 2 output = d, If Else = d
Input = , Switch 1 output = d, Switch 2 output = d, If Else = d
I'm only observing this behavior with Nullable types. Non-nullable types work as expected.
Note that this is not the same behavior that's in this question, and is not caused by this compiler bug that's been fixed in VS2015 Update 1. I've verified that both of those examples run correctly.
It looks like this is caused by this bug, and was fixed with this pull request.
I've tried a recent build of Roslyn and the sample code in the question works as expected now.
Here is the updated version of MSBuild Tools 2015 that has corrected the issue.
This is a bug. I believe it must be a left over of the same bug. You better replace your code with an If
to avoid any unexpected behavior.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With