คีย์เวิร์ดคือคำสงวนที่คอมไพเลอร์ C# กำหนดไว้ล่วงหน้า ไม่สามารถใช้เป็นตัวระบุ (identifier) ได้ หากต้องการใช้ ต้องเติมอักขระ @ นำหน้า
ใน C# บาง identifier มีความหมายพิเศษในบริบทของโค้ด เช่น get และ set เรียกว่า contextual keywords
ตารางคีย์เวิร์ดสงวน
| abstract | as | base | bool | break | byte | case |
| catch | char | checked | class | const | continue | decimal |
| default | delegate | do | double | else | enum | event |
| explicit | extern | false | finally | fixed | float | for |
| foreach | goto | if | implicit | in | in (generic modifier) | int |
| interface | internal | is | lock | long | namespace | new |
| null | object | operator | out | out (generic modifier) | override | params |
| private | protected | public | readonly | ref | return | sbyte |
| sealed | short | sizeof | stackalloc | static | string | struct |
| switch | this | throw | true | try | typeof | uint |
| ulong | unchecked | unsafe | ushort | using | virtual | void |
| volatile | while |
ตัวอย่างใช้ 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