Alex Rivera | Logout

TryParse() - Why does this code return 0, shouldn't the result be -1?

Asked 2012-06-13T09:57:14.867
8

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

Edit
Report

1 Answer

2

if it fails it will return false and the code in the if statement won't be executed.

In the case the conversion fails it will also retun 0 that's why output=0

answered 2012-06-13T09:59:26.380

Your Answer