Reputation: 11
Newbie here, any help understanding is appreciated. First experience with tryparse.
In this instance:
do
{
Console.Write("What is the temperature (in degrees Fahrenheit): ");
outcome = double.TryParse(Console.ReadLine(), out tempf);
if (outcome == false)
{
Console.WriteLine("Invalid input");
}
} while (outcome == false);
A user input of 'stack' would return a boolean
'false' value, whereas an input such as '100' would return as 'true'.
I am under the impression that double.TryParse
would only return true
if the user input is of string
type, as this would be a successful parse.
Upvotes: 1
Views: 614
Reputation: 4164
From msdn :
Double.TryParse
Converts the string representation of a number to its double-precision floating-point number equivalent. A return value indicates whether the conversion succeeded or failed
So as long as conversion is possible it would return true. For example user input is "123X" will fail try parse.
Upvotes: 1
Reputation: 7110
You are using double.TryParse
which means you are trying to parse inputed value to a double value. TryParse returns true if this can be done or it returns false.
As per your inputs, for user input of 'stack', TryParse is trying to parse it to a double value which is not a valid conversion and its failing and hence returning false. And for input such as '100', TryParse can do that perfectly.
Here is MSDN link for it
Upvotes: 0