c# - ReSharper 表示 Console.ReadLine() 返回空值

标签 c# visual-studio resharper

我注意到 ReSharper 建议我检查 Console.ReadLine() 是否为 null。我不明白为什么,因为据我所知,即使您在控制台中按 enter 并且不输入任何符号,该方法也会返回 ""

我使用 VS 2015 和第三次更新、C# 6、.NET 4.6.1、ReSharper 10。

enter image description here

最佳答案

The documentation指定返回 null 是此方法约定的一部分:

The next line of characters from the input stream, or null if no more lines are available.

接着举个例子:

If the Ctrl+Z character is pressed when the method is reading input from the console, the method returns null.

作为进一步的示例,您可以使用 Console.SetIn 更改用于 Console.InTextReader。当调用 ReadLine 时,您的 TextReader 可能会返回 null。

关于c# - ReSharper 表示 Console.ReadLine() 返回空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38370565/

相关文章:

c# - 如何使用 C# 取消散列?

visual-studio - 扩展 Win Embedded CE 6.0 的媒体功能

c# - 主要输出的 Visual Studio 部署项目快捷方式 - 'Open file location' 不工作

git - Visual Studio 忽略 .vs

visual-studio - Resharper 7.1解决方案分析错误

c# - 使用没有回发的按钮将旧设计更改为新设计

c# - 是否可以对 List<Interface> 施加类型约束?

visual-studio - Visual Studio 2012 : change setting for "Enable floating tab wells"

c# - ReSharper 警告 - 访问修改后的闭包

c# - 为什么 Collections 的评估顺序与使用条件运算符的其他类型不同