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.
I use VS 2015 with the 3rd update, C# 6, .NET 4.6.1, ReSharper 10.
Return Value: It returns the next line of characters of string type from the input stream, or null if no more lines are available.
readLine() ); you are at the second line of the file. At player = br. readLine(); you are reading the third line of the file. If there is only one line in the file, this line will return null.
The C# readline method is mainly used to read the complete string until the user presses the Enter key or a newline character is found. Using this method, each line from the standard data input stream can be read. It is also used to pause the console so that the user can take a look at the output.
The Console. ReadLine() method reads and only returns the string from the stream output device (console) until a newline character is found. If we want to read a character or numeric value from the user, we need to convert the string to the appropriate data sets.
The documentation specifies that returning null is part of the contract for this method:
The next line of characters from the input stream, or null if no more lines are available.
And goes onto give an example:
If the Ctrl+Z character is pressed when the method is reading input from the console, the method returns 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With