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

CsharpServer Side ProgrammingProgramming

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
raja
Published on 10-Dec-2019 07:32:04
Advertisements