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


C# Convert.ToBoolean(String, IFormatProvider)用法及代碼示例


此方法用於使用指定的區域性特定格式信息將邏輯值的指定字符串表示形式轉換為其等效的布爾值。

用法:

public static bool ToBoolean (string value, IFormatProvider provider);

參數:


  • value:它是一個字符串,其中包含TrueString或FalseString的值。
  • provider:它是提供區域性特定格式信息的對象。該參數被忽略。

返回值:如果值等於TrueString,則此方法返回true;如果值等於FalseString或null,則返回false。

異常:如果該值不等於TrueString或FalseString,則此方法將引發FormatException。

以下示例程序旨在說明Convert.ToBoolean(String,IFormatProvider)方法的用法:

範例1:

// C# program to demonstrate the 
// Convert.ToBoolean() Method 
using System; 
using System.Globalization; 
  
class GFG { 
  
// Main Method 
public static void Main() 
{ 
    try { 
          
        // creating object of CultureInfo 
        CultureInfo cultures = new CultureInfo("en-US"); 
  
        // declaring and intializing String array 
        String[] values = { null, "true",  
                    "False", " false " }; 
  
        // calling get() Method 
        Console.WriteLine("Converted bool value "+ 
                        "of specified strings:"); 
                          
        for (int j = 0; j < values.Length; j++) { 
            get(values[j], cultures); 
        } 
    } 
  
    catch (FormatException e) { 
  
        Console.Write("Exception Thrown:"); 
        Console.Write("{0}", e.GetType(), e.Message); 
    } 
} 
  
// Defining get() method 
public static void get(string s, CultureInfo cultures) 
{ 
  
    // converting string to specified bool 
    bool val = Convert.ToBoolean(s, cultures); 
  
    // display the converted string 
    Console.Write(" {0}, ", val); 
} 
}
輸出:
Converted bool value of specified strings:
 False,  True,  False,  False,

範例2:對於FormatException

// C# program to demonstrate the 
// Convert.ToBoolean() Method 
using System; 
using System.Globalization; 
class GFG { 
  
// Main Method 
public static void Main() 
{ 
    try { 
          
        // creating object of CultureInfo 
        CultureInfo cultures = new CultureInfo("en-US"); 
  
        // declaring and intializing String array 
        String[] values = { null, "true",  
                "False", " false ", "" }; 
  
        // calling get() Method 
        Console.WriteLine("Converted bool value"+ 
                      " of specified strings:"); 
                        
        for (int j = 0; j < values.Length; j++) { 
            get(values[j], cultures); 
        } 
    } 
  
    catch (FormatException e) { 
        Console.WriteLine("\n"); 
        Console.Write("Exception Thrown:"); 
        Console.Write("{0}", e.GetType(), e.Message); 
    } 
} 
  
// Defining get() method 
public static void get(string s, CultureInfo cultures) 
{ 
  
    // converting string to specified bool 
    bool val = Convert.ToBoolean(s, cultures); 
  
    // display the converted string 
    Console.Write(" {0}, ", val); 
} 
}
輸出:
Converted bool value of specified strings:
 False,  True,  False,  False, 

Exception Thrown:System.FormatException

參考:



相關用法


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