C
C#14mo ago
d3adin.

❔ 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:
if (readResult != null)
{
valueEntered = readResult;
}
if (readResult != null)
{
valueEntered = readResult;
}
Upon testing I can see that the code works exactly the same that being commented out. Here is the full program.
string? readResult;
string valueEntered = "";
int numValue = 0;
bool validNumber = false;

Console.WriteLine("Enter an integer value between 5 and 10");

do
{
readResult = Console.ReadLine();
if (readResult != null)
{
valueEntered = readResult;
}

validNumber = int.TryParse(valueEntered, out numValue);

if (validNumber == true)
{
if (numValue <= 5 || numValue >= 10)
{
validNumber = false;
Console.WriteLine($"You entered {numValue}. Please enter a number between 5 and 10.");
}
}
else
{
Console.WriteLine("Sorry, you entered an invalid number, please try again");
}
} while (validNumber == false);

Console.WriteLine($"Your input value ({numValue}) has been accepted.");

readResult = Console.ReadLine();
string? readResult;
string valueEntered = "";
int numValue = 0;
bool validNumber = false;

Console.WriteLine("Enter an integer value between 5 and 10");

do
{
readResult = Console.ReadLine();
if (readResult != null)
{
valueEntered = readResult;
}

validNumber = int.TryParse(valueEntered, out numValue);

if (validNumber == true)
{
if (numValue <= 5 || numValue >= 10)
{
validNumber = false;
Console.WriteLine($"You entered {numValue}. Please enter a number between 5 and 10.");
}
}
else
{
Console.WriteLine("Sorry, you entered an invalid number, please try again");
}
} while (validNumber == false);

Console.WriteLine($"Your input value ({numValue}) has been accepted.");

readResult = Console.ReadLine();
10 Replies
cap5lut
cap5lut14mo ago
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
Angius
Angius14mo ago
For the most part, it's perfectly fine to use either
var input = Console.ReadLine() ?? "";
var input = Console.ReadLine() ?? "";
or
var input = Console.ReadLine()!;
var input = Console.ReadLine()!;
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
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
cap5lut
cap5lut14mo ago
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 forever
d3adin.
d3adin.OP14mo ago
Thank you all, looks like I need to do some reading It's starting to make sense, until I tried this:
string input = Console.ReadLine();

if (input != null)
{
Console.WriteLine("You entered: " + input);
}
else
{
Console.WriteLine("You didn't enter anything.");
}
string input = Console.ReadLine();

if (input != null)
{
Console.WriteLine("You entered: " + input);
}
else
{
Console.WriteLine("You didn't enter anything.");
}
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 : "
cap5lut
cap5lut14mo ago
no, because pressing enter doesnt close the stream, it simply adds either \r\n or \n to the stream depending on ur operating system
❤RieBi&❤
❤RieBi&❤14mo ago
Yep, the Console.ReadLine() should return an empty string in that case
d3adin.
d3adin.OP14mo ago
Again, thank you so much. I finally understand it
Accord
Accord14mo ago
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.
Want results from more Discord servers?
Add your server