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

เมธอด Boolean.ToString() ใน C#

เมธอด Boolean.ToString() ใน C# ใช้แปลงค่าของอินสแตนซ์นี้เป็นการแทนค่าสตริงเทียบเท่า (คือ "True" หรือ "False")

ไวยากรณ์

ไวยากรณ์มีดังนี้:

public override string ToString ();

ตัวอย่าง

ตัวอย่างการใช้งานเมธอด Boolean.ToString() กับค่า true:

using System; 
public class Demo { 
    public static void Main(){ 
        bool b = true; 
        string res = b.ToString(); 
        Console.WriteLine("Return Value = " + res); 
    } 
}

ผลลัพธ์

โค้ดจะแสดงผลดังนี้:

Return Value = True

ตัวอย่างเพิ่มเติม

ตัวอย่างที่สองกับค่า false:

using System; 
public class Demo { 
    public static void Main(){ 
        bool b = false; 
        string res = b.ToString(); 
        Console.WriteLine("Return Value = " + res); 
    } 
}

ผลลัพธ์

โค้ดจะแสดงผลดังนี้:

Return Value = False