Ich sehe ein seltsames Verhalten bei der Verwendung einer Nullable Long Switch-Anweisung in VS2015 Update 1, die ich nicht in anderen Visual Studio-Versionen sehe, wo es wie erwartet ausgeführt wird.Nullable Long-Switch-Anweisung, die keine erwartete Ausgabe in VS2015 produziert
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);
}
Dieser Beispielcode erzeugt die folgende Ausgabe (zur besseren Lesbarkeit Aligned):
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
ich nur dieses Verhalten mit Nullable-Typen zu beobachten. Nicht nullfähige Typen funktionieren wie erwartet.
Hinweis, dass dies nicht das gleiche Verhalten, das in this question ist, und wird nicht durch this Compiler Fehler verursacht, die in VS2015 Update behoben worden sind 1. Ich habe festgestellt, dass diese beiden Beispiele ordnungsgemäß ausgeführt.