Nothing Really Matters
What is “null”?
// Declare variables
string? readResult; // Variable to hold user input (nullable string)
In the above the ? is an operator that allows for null input, in this instance either a valid string, or “null”
This allows for us to subsequently execute boolean operations on the input
Continue to declare variables:
string valueEntered = ""; // Variable to hold (store)user input as string
int numValue = 0; // Variable to hold parsed integer value
bool validNumber = false; // Flag to track if the entered number is valid
Console.WriteLine("Enter an integer value between 5 and 10");// Prompt user to enter an integer value between 5 and 10
// Start a do-while loop to handle user input validation
do
{
readResult = Console.ReadLine();// Read user input from console
Refering back to the nullable string declared in the first variable:
if (readResult != null) // If user input is not null, assign it to valueEntered
this element of the “if” statement checks that the input is NOT null, and if so, continues:
{
valueEntered = readResult;
}
validNumber = int.TryParse(valueEntered, out numValue);// Attempt to parse the valueEntered string to an integer
if (validNumber == true)// Check if parsing was successful
{
// Check if the parsed number is within the desired range
if (numValue <= 5 || numValue >= 10)
{
validNumber = false;/ If the number is not within the range, set validNumber to false
Console.WriteLine($"You entered {numValue}. Please enter a number between 5 and 10.");// Prompt the user to enter a number between 5 and 10
}
}
else
{
// If parsing fails, inform the user that an invalid number was entered
Console.WriteLine("Sorry, you entered an invalid number, please try again");
}
} while (validNumber == false); // Repeat the loop until a valid number is entered
Console.WriteLine($"Your input value ({numValue}) has been accepted.");// Display a message confirming acceptance of the input value
readResult = Console.ReadLine();// Read an additional input (unnecessary in this context)
Microsoft Learn:C# Nullable reference types