C# | Boolean.TryParse() Method

This method is used to convert the specified string representation of a logical value to its Boolean equivalent. It returns a value that indicates whether the conversion succeeded or failed.

Syntax:

public static bool TryParse (string value, out bool result);

Parameters:



value: It is a string containing the value to convert.

result: When this method returns, if the conversion succeeded, contains true if value is equal to TrueString or false if value is equal to FalseString. If the conversion failed, contains false. The conversion fails if the value is null or is not equal to the value of either the TrueString or FalseString field.

Return Value: This method returns true if value was converted successfully otherwise it returns false.

Below programs illustrate the use of Boolean.TryParse(String, Boolean) Method:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate
// Boolean.TryParse(String, Boolean)
// Method
using System;
  
class GFG {
  
// Main Method
public static void Main() {
  
        // passing different values 
        // to the method to check
        checkParse("true");
        checkParse("false");
        checkParse("'     true     '");
        checkParse(" $  ");
        checkParse("1");
    }
  
// Declaring checkparse method
public static void checkParse(string value) {
  
        // Declaring data type
        bool result;
        bool flag;
   
        // using the method
        result = Boolean.TryParse(value, out flag);
  
        // Display boolean type result
        Console.WriteLine("{0} --> {1} ", value, result);
    }
}

chevron_right


Output:

true --> True 
false --> True 
'     true     ' --> False 
 $   --> False 
1 --> False

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate
// Boolean.TryParse(String, Boolean)
// Method
using System;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // passing different values 
        // to the method to check
        checkParse("true1");
        checkParse(null);
        checkParse(String.Empty);
    }
  
// Declaring checkparse method
public static void checkParse(string value) {
  
        // Declaring data type
        bool result;
        bool flag;
   
        // using the method
        result = Boolean.TryParse(value, out flag);
  
        // Display boolean type result
        Console.WriteLine("{0} --> {1} ", value, result);
    }
}

chevron_right


Output:

true1 --> False 
 --> False 
 --> False

Note: The TryParse method is like the Parse method, except the TryParse method does not throw an exception if the conversion fails.

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.