Useful tips while using bool.TryParse method


Case I:

bool temp = bool.TryParse(“False”, out temp);

Console.WriteLine(temp);

Will always return true

Because bool.TryParse return true if string can be parsed into a bool and

the second parameter to TryParse is an out parameter the TryParse method is forced to initialize the parameter.

And after that we initialize same parameter with the value return by bool.TryParse so it will be set as true.

Case II:

bool temp;

bool.TryParse(“False”, out temp);

Console.WriteLine(temp);

In this case it will return false.

Hope this help!