Nullable Long switch 语句在 VS2015 中没有产生预期的输出
Nullable Long switch statement not producing expected output in VS2015
我在 VS2015 Update 1 中使用可为 null 的长 switch 语句时看到一些奇怪的行为,而我在其他 Visual Studio 版本中没有看到它 运行 符合预期。
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);
}
此示例代码产生以下输出(为便于阅读而对齐):
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
我只观察到 Nullable 类型的这种行为。不可为 null 的类型按预期工作。
请注意,这 不是 this question, and is not caused by this VS2015 Update 1 中已修复的编译器错误中的相同行为。我已验证这两个示例 运行 正确。
这是一个错误。我相信它一定是相同 bug 的遗留物。您最好用 If
替换您的代码以避免任何意外行为。
看来这是由this bug, and was fixed with this pull request引起的。
我尝试了 Roslyn 的最新版本,问题中的示例代码现在按预期工作。
Here 是已更正该问题的 MSBuild Tools 2015 更新版本。
我在 VS2015 Update 1 中使用可为 null 的长 switch 语句时看到一些奇怪的行为,而我在其他 Visual Studio 版本中没有看到它 运行 符合预期。
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);
}
此示例代码产生以下输出(为便于阅读而对齐):
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
我只观察到 Nullable 类型的这种行为。不可为 null 的类型按预期工作。
请注意,这 不是 this question, and is not caused by this VS2015 Update 1 中已修复的编译器错误中的相同行为。我已验证这两个示例 运行 正确。
这是一个错误。我相信它一定是相同 bug 的遗留物。您最好用 If
替换您的代码以避免任何意外行为。
看来这是由this bug, and was fixed with this pull request引起的。
我尝试了 Roslyn 的最新版本,问题中的示例代码现在按预期工作。
Here 是已更正该问题的 MSBuild Tools 2015 更新版本。