Archive

Posts Tagged ‘Temporary Employment’

Useful tips while using bool.TryParse method

November 23, 2012 Leave a comment

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!