C# Convert ToBoolean(String)

Description

Convert ToBoolean(String) converts the specified string representation of a logical value to its Boolean equivalent.

Syntax

Convert.ToBoolean(String) has the following syntax.


public static bool ToBoolean(
  string value
)

Parameters

Convert.ToBoolean(String) has the following parameters.

  • value - A string that contains the value of either Boolean.TrueString or Boolean.FalseString.

Returns

Convert.ToBoolean(String) method returns true if value equals TrueString, or false if value equals FalseString or null.

Example

The following example uses the Convert.ToBoolean(String) method to convert various strings to Boolean values.


using System;/*  w w  w. java 2 s  .  c  o  m*/

public class BooleanConversion
{
   public static void Main()
   {
      ConvertToBoolean(null);
      ConvertToBoolean(String.Empty);
      ConvertToBoolean("true");
      ConvertToBoolean("False");
      ConvertToBoolean("0");
   }

   private static void ConvertToBoolean(string value)
   {
      try
      {
         Console.WriteLine("Converted '{0}' to {1}.", value,  
                           Convert.ToBoolean(value));
      }
      catch (FormatException)
      {
         Console.WriteLine("Unable to convert '{0}' to a Boolean.", value);
      }
   }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version