string to bool c# code example

Example 1: how to convert string to bool c#

string sample = "True";
 bool myBool = bool.Parse(sample);

 ///or

 bool myBool = Convert.ToBoolean(sample);

Example 2: c# string to bool

Console.WriteLine(Boolean.TryParse("false", out bool myBool));
// Output: True
// If you want to use "false" in its boolean variable, try:
Console.WriteLine(myBool);
// Output: False

Example 3: convert string to boolean c#

bool b = str == "1";

Example 4: c# bool? to bool

bool newBool = nullableBoolean ?? false;