Skip to content
Related Articles

Related Articles

Improve Article

C# | Boolean.Parse() Method

  • Last Updated : 13 Sep, 2021
Geek Week

This method is used to convert the specified string representation of a logical value to its Boolean equivalent.
 

Syntax:  

public static bool Parse (string value);

Here, the value is the string which contains the value to convert.
Return Value: This method returns true if value is equivalent to TrueString false if value is equivalent to FalseString.
Exceptions:  

  • ArgumentNullException: If the string value is null.
  • FormatException: If the value is not equivalent to TrueString or FalseString.

Below programs illustrate the use of Boolean.Parse(String) Method:
Example 1: 

CSHARP




// C# program to demonstrate
// Boolean.Parse(String)
// Method
using System;
 
class GFG {
 
    // Main Method
    public static void Main()
    {
 
        try {
 
            // passing different values
            // to the method to check
            checkParse("true");
            checkParse("TRUE");
            checkParse("false");
            checkParse("FALSE");
            checkParse(bool.TrueString);
            checkParse(bool.FalseString);
        }
 
        catch (ArgumentNullException e) {
 
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
        catch (FormatException e) {
 
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
 
    // Defining checkParse method
    public static void checkParse(string input)
    {
 
        // declaring bool variable
        bool val;
 
        // getting parsed value
        val = bool.Parse(input);
        Console.WriteLine("'{0}' parsed as {1}", input, val);
    }
}
Output: 



'true' parsed as True
'TRUE' parsed as True
'false' parsed as False
'FALSE' parsed as False
'True' parsed as True
'False' parsed as False

 

Example 2: For ArgumentNullException

CSHARP




// C# program to demonstrate
// Boolean.Parse(String)
// Method for ArgumentNullException
using System;
 
class GFG {
 
    // Main Method
    public static void Main()
    {
 
        try {
 
            // passing null value as a input
            checkParse(null);
        }
        catch (ArgumentNullException e) {
 
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
        catch (FormatException e) {
 
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
 
    // Defining checkparse method
    public static void checkParse(string input)
    {
 
        // declaring bool variable
        bool val;
 
        // getting parsed value
        val = bool.Parse(input);
        Console.WriteLine("'{0}' parsed as {1}", input, val);
    }
}
Output: 
Exception Thrown: System.ArgumentNullException

 

Example 3: For FormatException

CSharp




// C# program to demonstrate
// Boolean.Parse(String)
// Method for FormatException
using System;
 
class GFG {
 
    // Main Method
public
    static void Main()
    {
 
        try {
 
            // passing true is not equivalent
            // to true value as a input
            checkParse("true");
        }
 
        catch (ArgumentNullException e) {
 
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
        catch (FormatException e) {
 
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
 
    // Defining checkParse method
    public static void checkParse(string input)
    {
 
        // declaring bool variable
        bool val;
 
        // getting parsed value
        val = bool.Parse(input);
        Console.WriteLine("'{0}' parsed as {1}", input, val);
    }
}
Output: 
Exception Thrown: System.FormatException

 

Note: The value parameter, optionally preceded or trailed by white space, must contain either TrueString or FalseString otherwise, it will throw an exception and the comparison is case-insensitive.
Reference: 




My Personal Notes arrow_drop_up
Recommended Articles
Page :