Advertisements
Home > ASP.NET > Useful tips while using bool.TryParse method

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!

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: