Convert a specified value to an equivalent Boolean value in C#


To convert a specified value to an equivalent Boolean value, the code is as follows −

Example

 Live Demo

using System;
using System.Globalization;
public class Demo {
   public static void Main() {
      CultureInfo cultures = new CultureInfo("en-US");
      String str = "true";
      Console.WriteLine("Converted bool value...");
      bool res = Convert.ToBoolean(str, cultures);
      Console.Write("{0}", res);
   }
}

Output

This will produce the following output −

Converted bool value...
True

Example

Let us see another example −

 Live Demo

using System;
using System.Globalization;
public class Demo {
   public static void Main() {
      CultureInfo cultures = new CultureInfo("es-ES", false);
      String str = "false";
      Console.WriteLine("Converted bool value...");
      bool res = Convert.ToBoolean(str, cultures);
      Console.Write("{0}", res);
   }
}

Output

This will produce the following output −

Converted bool value...
False

Updated on: 10-Dec-2019

237 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements