Parse string to bool value

In this chapter you will learn:

  1. Converts string to Boolean, throws an exception if not compatible

Convert string to bool

using System;/*j a  v a 2s.  c o m*/

public class Example
{
   public static void Main()
   {
       try {
          bool flag = Boolean.Parse("True");
          flag = Boolean.Parse("False");
          flag = Boolean.Parse("true");
          flag = Boolean.Parse("false");
          flag = Boolean.Parse(" true ");
          flag = Boolean.Parse("0");
          flag = Boolean.Parse("1");
          flag = Boolean.Parse("-1");
          flag = Boolean.Parse("asdf");
       }
       catch (ArgumentException) {
          Console.WriteLine("Cannot parse a null string.");
       }   
       catch (FormatException) {
          Console.WriteLine("Cannot parse '{0}'.", value);
       }         
                                         
   }
}

Next chapter...

What you will learn in the next chapter:

  1. Get to know C# char type
  2. Assign value to a char type
Home » C# Tutorial » bool, char
bool
bool and if statement
bool value to string
Parse string to bool value
char type
char escape sequence
char case
Compare char value
char min/max value
Is char a digit
Is char a letter
Is char a symbol
Is char a Separator
Is char white space
Convert to char
UTF-16 Character