C# 中的 Convert.ToBoolean(String, IFormatProvider) 方法


C# 中的 Convert.ToBoolean() 方法用於將指定值轉換為等效的布林值。

語法

以下為語法 −

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

在上面,Val 是一個包含 TrueString 或 FalseString 值的字串,而提供程式是一個提供特定於文化格式資訊的的 objects。

示例

現在我們來看一個例子來實現 Convert.ToBoolean() 方法 −

using System;
using System.Globalization;
public class Demo {
   public static void Main(){
      CultureInfo cultures = new CultureInfo("en-US");
      String str = "true";
      Console.WriteLine("Converted bool value...");
      bool res = Convert.ToBoolean(str, cultures);
      Console.Write("{0}", res);
   }
}

輸出

這將產生以下輸出 −

Converted bool value...
True

更新於: 2019 年 11 月 5 日

299 次瀏覽

開創您的 職業生涯

完成課程並獲取認證

開始
廣告
© . All rights reserved.