the bool.Parse() method to convert a string to a boolean value.
xxxxxxxxxx
string str = "True";
bool b = bool.Parse(str);
//the string "True" will be converted to the boolean value true.
//If you pass in a string that is not a valid boolean value ("True" or "False"),
//then an FormatException will be thrown.
//You can also use the bool.TryParse() method, which returns a boolean
//indicating whether the conversion succeeded or failed, without throwing an
//exception. Here's an example:
string str = "False";
bool result;
if (bool.TryParse(str, out result))
{
Console.WriteLine("Conversion succeeded. Result: {0}", result);
}
else
{
Console.WriteLine("Conversion failed.");
}
the string "False" will be converted to the boolean value false, and the bool.TryParse() method will return true. If you pass in a string that is not a valid boolean value, the method will return false, and the result variable will contain the default value of false.
xxxxxxxxxx
string sample = "True";
bool myBool = bool.Parse(sample);
///or
bool myBool = Convert.ToBoolean(sample);
xxxxxxxxxx
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
xxxxxxxxxx
using System;
public class myExample {
public static void Main(){
bool boolVal = true;
string strBool = boolVal.ToString();
Console.WriteLine(strBool);
}
}