I have written two methods and added an infinite while loop in the method.
- The first method
public static int GetNumber()
{
bool isCountinue = true;
while (isCountinue)
{
Console.Write("Please enter an integer number: ");
string value = Console.ReadLine();
bool isInteger = int.TryParse(value, out int number);
if (isInteger)
{
return number;
}
else
{
Console.WriteLine("Your input is not an integer number!");
isCountinue = false;
}
}
}
- The second method
public static int GetNumber()
{
while (true)
{
Console.Write("Please enter an integer number: ");
string value = Console.ReadLine();
bool isInteger = int.TryParse(value, out int number);
if (isInteger)
{
return number;
}
else
{
Console.WriteLine("Your input is not an integer number!");
continue;
}
}
}
I have gotten an error for the first method. Because it is missing the return value in the first method. I am confused why the second method is correct, and the first method is wrong?