❔ Console.ReadLine != null
Hi all,
I'm went trough this Microsoft challenge (unsuccessfully) and after reviewing this solution I'm trying to understand what the purpose is of:
Upon testing I can see that the code works exactly the same that being commented out. Here is the full program.
10 Replies
processes have an input stream, thats what
Console.ReadLine()
and a like use to get user input.
if that stream is closed Console.ReadLine()
will return null
this can for example happen if u pipe output from one process as input to another process processA | processB
For the most part, it's perfectly fine to use either
or
to strip away the nullability. In the first case, in case of a null you'll get an empty string. In the second case, you're telling the compiler "trust me bro, it's not null"
Unknown User•14mo ago
Message Not Public
Sign In & Join Server To View
in the end if
Console.ReadLine()
returns null
, the whole application can exit.
u cant reopen that input stream, thus u will forever only get null
back.
so right now, if the input stream was closed it would spam Sorry, you entered an invalid number, please try again
foreverThank you all, looks like I need to do some reading
It's starting to make sense, until I tried this:
In theory if I just press enter without any input I should see "You didn't enter anything.". However, it comes back with "You entered : "
no, because pressing enter doesnt close the stream, it simply adds either
\r\n
or \n
to the stream depending on ur operating systemYep, the Console.ReadLine() should return an empty string in that case
Again, thank you so much. I finally understand it
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.