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

ค่าเริ่มต้น (Default Value) ของ bool ใน C#


ภาพรวม

ใช้ operator default เพื่อดึงค่าเริ่มต้นของชนิดข้อมูล สำหรับ bool ค่าเริ่มต้นคือ false

รูปแบบการใช้งาน

bool a = default(bool); // false
// หรือ C# 7.1+:
bool b = default; // type inference

ตัวอย่างโค้ดเต็ม

using System;

public class Demo
{
    public static void Main()
    {
        bool a = default(bool);
        Console.WriteLine("Default for bool type = " + a);
    }
}

ผลลัพธ์

Default for bool type = False

ตารางค่าเริ่มต้นของชนิดพื้นฐาน

ชนิดDefault Value
boolfalse
byte, short, int, long0
float, double0.0
decimal0.0M
char'\0' (null char)
reference types (string, class, array, delegate)null
structstruct ที่ทุก field เป็น default