當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C# Boolean.Parse()用法及代碼示例

此方法用於將指定的邏輯值的字符串表示形式轉換為其等效的布爾值。

用法:

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

注意:值參數(可以選擇在空格前或後)必須包含TrueString或FalseString,否則將引發異常,並且比較為大小寫敏感。

參考:



相關用法


注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 C# | Boolean.Parse() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。