📜  C#| Boolean.Parse()方法

📅  最后修改于: 2021-05-30 01:43:18             🧑  作者: Mango

此方法用于将指定的逻辑值的字符串表示形式转换为其等效的布尔值。

句法:

public static bool Parse (string value);

在此,是包含要转换的的字符串。

返回值:如果value等于TrueString,则此方法返回true;如果value等于FalseString,则此方法返回false

例外情况:

  • ArgumentNullException:如果字符串值为null。
  • FormatException:如果该不等于TrueString或FalseString。

下面的程序说明了Boolean.Parse(String)方法的用法:

范例1:

// 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);
    }
}
输出:
'true' parsed as True
'TRUE' parsed as True
'false' parsed as False
'FALSE' parsed as False
'True' parsed as True
'False' parsed as False

示例2:对于ArgumentNullException

// 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);
    }
}
输出:
Exception Thrown: System.ArgumentNullException

示例3:对于FormatException

// C# program to demonstrate
// Boolean.Parse(String)
// Method for FormatException
using System;
  
class GFG {
  
    // Main Method
public
    static void Main()
    {
  
        try {
  
            // passing truee is not equivalent
            // to true value as a input
            checkParse("truee");
        }
  
        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

注意: value参数(可以选择在空格之前或之后)必须包含TrueString或FalseString,否则将引发异常,并且比较不区分大小写。

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.boolean.parse?view=netframework-4.7.2