Convert string to Boolean. A return value indicates whether the conversion succeeded or failed. : bool « Data Types « C# / C Sharp






Convert string to Boolean. A return value indicates whether the conversion succeeded or failed.

  

using System;

public class Example
{
   public static void Main()
   {
      string[] values = { "True", "False", "true", "false", "true ", "0", "1", "-1", "string" };
      foreach (var value in values) {
         bool flag;
         if (Boolean.TryParse(value, out flag))
            Console.WriteLine("'{0}' --> {1}", value, flag);
         else
            Console.WriteLine("Unable to parse '{0}'.", 
                              value == null ? "<null>" : value);
      }                                     
   }
}

   
    
  








Related examples in the same category

1.bool variable
2.A static method that returns a Boolean value.
3.Using BoolUsing Bool
4.Demonstrate bool valuesDemonstrate bool values
5.Print a truth table for the logical operatorsPrint a truth table for the logical operators
6.bool FalseString, TrueString
7.Convert boolean value to "Yes" or "No"
8.Converts string to Boolean, throws an exception if not compatible
9.Convert text values to boolean