C
C#7mo 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
cap5lut7mo 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
Angius7mo 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 User7mo ago
Message Not Public
Sign In & Join Server To View
cap5lut
cap5lut7mo 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.7mo 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
cap5lut7mo 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&❤7mo ago
Yep, the Console.ReadLine() should return an empty string in that case
d3adin.
d3adin.7mo ago
Again, thank you so much. I finally understand it
Accord
Accord7mo 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
More Posts
✅ New to wpf and need some help with architecture.I have a game-listener that gets new information every second. I crreate an instance and start the ✅ Kinda new to MVC/C#/Razor - Need some help passing data from Model -> ControllerAs the title states, I'm kinda new to C#/MVC with a good bit of experience with software development✅ WPF Custom control should have contents as DockPanel, but it shows up at runtime as ScrollViewerI have a `UserControl` I'm implementing with the source shown below. When I check my custom controlrider dont see the android sdkI download android sdk, but rider dont see it✅ Dotnet build failure on Linux due to nuget path case sensitivityAt work we're mostly using Windows, but we're trying to setup `Integration Tests` with the `TestCont✅ WPF MouseOver expansion of item isn't workingI have a window with a bar that needs to hide down to a separator until the user mouses over it or c❔ Entity Framework always stuck, how can I debug?Everything I do gets stuck like this (adding migrations/updating database). How can I debug this is❔ Program fails with reference when private=False but works when private=True''' <ItemGroup> <Reference Include="Custom"> <HintPath>C:\SomeCustom.dll</HintPath> Cannot read sudden imagesI am trying to copy an image and then use that copied version of the image. **BitmapImage** gives an❔ convert http into ws? asp.net core web apiHello Everyone! i'm new into C# and I wonder how to create a websocket server in asp.net core web ap