sst*_*tan 5 c# casting string-interpolation c#-6.0 visual-studio-2015
考虑一下这个在Visual Studio 2015中编译得很好的简单程序:
public class Program
{
enum Direction
{
Up,
Down,
Left,
Right
}
static void Main(string[] args)
{
// Old style
Console.WriteLine(string.Format("The direction is {0}", Direction.Right));
Console.WriteLine(string.Format("The direction is {0}", (int)Direction.Right));
// New style
Console.WriteLine($"The direction is {Direction.Right}");
Console.WriteLine($"The direction is {(int)Direction.Right}");
}
}
Run Code Online (Sandbox Code Playgroud)
...按预期输出:
The direction is Right
The direction is 3
The direction is Right
The direction is 3
Run Code Online (Sandbox Code Playgroud)
但是,Visual Studio 2015特别建议在此行中建议"快速操作":
// "Cast is redundant" warning
Console.WriteLine($"The direction is {(int)Direction.Right}");
Run Code Online (Sandbox Code Playgroud)
它坚持认为(int) "演员是多余的",并建议作为"删除不必要的演员"的潜在修复,这当然是错误的,因为它会改变结果.
有趣的是,它没有给我任何相应声明的警告:
// No warnings.
Console.WriteLine(string.Format("The direction is {0}", (int)Direction.Right));
Run Code Online (Sandbox Code Playgroud)
当在插值字符串中使用表达式时,有人能为这种误报提供合理的解释吗?
| 归档时间: |
|
| 查看次数: |
1065 次 |
| 最近记录: |