我注意到 ReSharper 建议我检查 Console.ReadLine()
是否为 null。我不明白为什么,因为据我所知,即使您在控制台中按 enter
并且不输入任何符号,该方法也会返回 ""
。
我使用 VS 2015 和第三次更新、C# 6、.NET 4.6.1、ReSharper 10。
最佳答案
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.In
的 TextReader
。当调用 ReadLine
时,您的 TextReader
可能会返回 null。
关于c# - ReSharper 表示 Console.ReadLine() 返回空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38370565/