VS2015 Update 1でNullable long switchステートメントを使用すると、期待通りに動作しない他のVisual Studioリリースで表示されない場合があります。私だけのNullable型と、この動作を観察していNullable長いswitchステートメントがVS2015で期待された出力を出力しない
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
:
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);
}
このサンプルコードは(読みやすくするために整列された)次の出力を生成します。 nullableでない型は、期待どおりに動作します。
これはないthis questionでだ、と私はこれらの例の両方が正しく動作することを確認したVS2015アップデート1で修正されていますthisコンパイラのバグが原因ではないと同じ動作である。なお。