ReSharper 说 Console.ReadLine() 返回空值 [英] ReSharper says that Console.ReadLine() returns null value
问题描述
我注意到 ReSharper 建议我检查 Console.ReadLine()
是否为 null.我不明白为什么,因为据我所知,即使您在控制台中按 enter
并且不输入任何符号,该方法也会返回 ""
.
I noticed that ReSharper suggests me to check Console.ReadLine()
for null. I don't understand why, because as far as I know the method returns ""
even if you press enter
in a console and don't enter any symbol.
我使用 VS 2015 和第三次更新、C# 6、.NET 4.6.1、ReSharper 10.
I use VS 2015 with the 3rd update, C# 6, .NET 4.6.1, ReSharper 10.
推荐答案
文档指定返回 null 是此方法合同的一部分:
The documentation specifies that returning null is part of the contract for this method:
输入流中的下一行字符,如果没有更多行可用,则为null.
The next line of characters from the input stream, or null if no more lines are available.
然后举个例子:
如果在方法从控制台读取输入时按下 Ctrl+Z 字符,方法返回 null.
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.
As a further example, you can change the TextReader
used for Console.In
using Console.SetIn
. Your TextReader
could return null when ReadLine
is called.
这篇关于ReSharper 说 Console.ReadLine() 返回空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!