ReSharper 说 Console.ReadLine() returns 空值

ReSharper says that Console.ReadLine() returns null value

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

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

The documentation 指定 returning 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 可能 return 为空。

根据 MSDN,如果您已将标准输入重定向为来自文件,则 Console.ReadLine() 将 return null 当没有剩余行可读时来自文件。

Console.ReadLine()可以null按Ctrl+Z.

MSDN documentation:

If the Ctrl+Z character is pressed when the method is reading input from the console, the method returns null. This enables the user to prevent further keyboard input when the ReadLine method is called in a loop.