Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReSharper says that Console.ReadLine() returns null value

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.

enter image description here

like image 329
user2216 Avatar asked Jul 14 '16 09:07

user2216


People also ask

What is the return value of method console ReadLine ()?

Return Value: It returns the next line of characters of string type from the input stream, or null if no more lines are available.

Can ReadLine return null?

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.

What is the use of console ReadLine () in C#?

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.

Does console ReadLine return strings?

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.


1 Answers

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.

like image 87
Charles Mager Avatar answered Oct 11 '22 02:10

Charles Mager