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

คีย์เวิร์ดสงวน (Reserved Keywords) ใน C#

คีย์เวิร์ดคือคำสงวนที่คอมไพเลอร์ C# กำหนดไว้ล่วงหน้า ไม่สามารถใช้เป็นตัวระบุ (identifier) ได้ หากต้องการใช้ ต้องเติมอักขระ @ นำหน้า

ใน C# บาง identifier มีความหมายพิเศษในบริบทของโค้ด เช่น get และ set เรียกว่า contextual keywords

ตารางคีย์เวิร์ดสงวน

abstractasbaseboolbreakbytecase
catchcharcheckedclassconstcontinuedecimal
defaultdelegatedodoubleelseenumevent
explicitexternfalsefinallyfixedfloatfor
foreachgotoifimplicitinin (generic modifier)int
interfaceinternalislocklongnamespacenew
nullobjectoperatoroutout (generic modifier)overrideparams
privateprotectedpublicreadonlyrefreturnsbyte
sealedshortsizeofstackallocstaticstringstruct
switchthisthrowtruetrytypeofuint
ulonguncheckedunsafeushortusingvirtualvoid
volatilewhile

ตัวอย่างใช้ bool

using System;
using System.Collections;

class Demo {
    static void Main() {
        bool[] arr = new bool[5];
        arr[0] = true;
        arr[1] = true;
        arr[2] = false;
        arr[3] = false;
        BitArray bArr = new BitArray(arr);
        foreach (bool b in bArr) {
            Console.WriteLine(b);
        }
        bool str = arr[1];
        Console.WriteLine("Value of 2nd element: " + str);
    }
}

ผลลัพธ์

True
True
False
False
False
Value of 2nd element: True