See the following code:
string test = "";
int output = -1;
if (int.TryParse(test, out output))
{
Console.WriteLine("Parsed");
}
Console.WriteLine(output);
When TryParse()
fails, shouldn't the block be skipped over, Console.WriteLine("Parsed")
not called and the value of output
be the same (-1)?
It's returning 0
TryParse(String, Int32) Converts the string representation of a number to its 32-bit signed integer equivalent. A return value indicates whether the conversion succeeded.
TryParse is . NET C# method that allows you to try and parse a string into a specified type. It returns a boolean value indicating whether the conversion was successful or not. If conversion succeeded, the method will return true and the converted value will be assigned to the output parameter.
The TryParse() method receives two parameters. The first parameter is the string to be converted. The second parameter is the variable to store the converted value.
TryParse method converts a string value to a corresponding 32-bit signed integer value data type. It returns a Boolean value True , if conversion successful and False , if conversion failed. In case of failed conversion, it doesn't throw any exception and 0 is assigned to the out variable.
From MSDN:
When this method returns, contains the 32-bit signed integer value equivalent to the number contained in string, if the conversion succeeded, or
zero
if the conversion failed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With