Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

เมธอด Convert.ToBoolean(String, IFormatProvider) ใน C#

เมธอด Convert.ToBoolean() ใน C# ใช้แปลงค่าที่ระบุให้เป็นค่า Boolean (True/False) เทียบเท่า

ไวยากรณ์

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

val คือสตริงที่มีค่าเป็น TrueString หรือ FalseString provider ให้ข้อมูลการจัดรูปแบบเฉพาะวัฒนธรรม

ตัวอย่าง

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

ค่าที่ยอมรับ: "true", "false" (ไม่สนใจตัวพิมพ์เล็ก/ใหญ่) ค่าอื่นจะโยน FormatException