Boolean.TryParse() Method in C#


The Boolean.TryParse() method in C# is used to convert the specified string representation of a logical value to its Boolean equivalent.

Syntax

Following is the syntax −

public static bool TryParse (string val out bool result);

Example

Let us now see an example to implement the Boolean.TryParse() method −

using System;
public class Demo {
   public static void Main(){
      bool val;
      bool flag;
      val = Boolean.TryParse("true", out flag);
      Console.WriteLine("Result = "+val);
   }
}

Output

This will produce the following output −

Result = True

Example

Let us now see another example to implement the Boolean.TryParse() method −

using System;
public class Demo {
   public static void Main(){
      bool val;
      bool flag;
      val = Boolean.TryParse("$", out flag);
      Console.WriteLine("Result = "+val);
   }
}

Output

This will produce the following output −

Result = False

Updated on: 11-Nov-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements