C# | Boolean.Parse() Method - GeeksforGeeks

13 Sep, 2021
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)// Methodusing 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);    }}
'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 ArgumentNullExceptionusing 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);    }}
Exception Thrown: System.ArgumentNullException
 
Example 3: For FormatException
CSharp
// C# program to demonstrate// Boolean.Parse(String)// Method for FormatExceptionusing System; class GFG {     // Main Methodpublic    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);    }}
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: 
https://docs.microsoft.com/en-us/dotnet/api/system.boolean.parse?view=netframework-4.7.2
surinderdawra388
CSharp Boolean Struct
CSharp-method
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
C# | Method Overriding
C# Dictionary with examples
Difference between Ref and Out keywords in C#
C# | Delegates
Top 50 C# Interview Questions & Answers
C# | Constructors
Extension Method in C#
Introduction to .NET Framework
C# | Abstract Classes
C# | Class and Object