คีย์เวิร์ดสำคัญใน C# ที่ควรจดจำ
คีย์เวิร์ดเหล่านี้ใช้บ่อยและมีผลกระทบต่อการออกแบบโค้ดอย่างมาก
1. sealed - ป้องกันการ Override/Inherit
- วางหน้า
class: ไม่ให้สืบทอด (Non-inheritable) - วางหน้า
method/property: ไม่ให้ Override เพิ่ม (ต้องเป็น override แล้ว)
sealed class FinalClass { } // ไม่สามารถ inherit ได้
class Base
{
public virtual void Method() { }
}
class Derived : Base
{
public sealed override void Method() { } // ไม่ให้ override ต่อ
}
2. params - พารามิเตอร์จำนวนไม่จำกัด
- ต้องเป็นพารามิเตอร์ **ตัวสุดท้าย** เท่านั้น
- รับได้ทั้ง Array และ Comma-separated values
- ในเมธอดเดียวมีได้แค่ 1 params parameter
void PrintNumbers(params int[] numbers)
{
foreach (int n in numbers) Console.Write($"{n} ");
Console.WriteLine();
}
// เรียกใช้
PrintNumbers(1, 2, 3);
PrintNumbers(new int[] { 4, 5, 6 });
PrintNumbers(); // Empty array
3. internal - Surface Assembly
- เข้าถึงได้ **ภายใน Assembly เดียวกัน** (DLL/EXE เดียว)
- Default accessibility สำหรับ class/struct ที่ไม่ระบุ modifier
protected internal= protected OR internalprivate protected= protected AND internal (C# 7.2+)
// Assembly A
public class PublicClass { } // เข้าถึงได้จาก Assembly B
internal class InternalClass { } // เข้าถึงได้เฉพาะ Assembly A
public class Pub { internal void M() } // M เข้าถึงได้เฉพาะ Assembly A
4. this - อ้างอิง Instance ปัจจุบัน
- ใน Instance method/property: อ้างอิง this object
- แยก field และ parameter ที่ชื่อเหมือนกัน
- Indexer:
this[int index] - Extension method:
this Type name(static class) - Constructor chaining:
this(...)
class Person
{
string name;
public Person(string name) // parameter ชื่อซ้ำ field
{
this.name = name; // this.name = field, name = parameter
}
// Indexer
public string this[int index] => index == 0 ? name : "";
// Constructor chaining
public Person() : this("Unknown") { }
}
// Extension method
static class StringExtensions
{
public static bool IsNullOrEmpty(this string s) => string.IsNullOrEmpty(s);
}
// ใช้งาน
"hello".IsNullOrEmpty();
5. abstract - Abstract Class/Method
abstract class: ไม่สามารถ new ได้ ต้องสืบทอดabstract method: ไม่มี body ต้อง override ใน derived (non-abstract)- Abstract class สามารถมี method ปกติ (non-abstract) ร่วมด้วย
abstract class Shape
{
public abstract double Area(); // ต้อง override
public void Print() => Console.WriteLine($"Area: {Area()}"); // มี body
}
class Circle : Shape
{
public double Radius { get; set; }
public override double Area() => Math.PI * Radius * Radius;
}
คีย์เวิร์ดสำคัญอื่นๆ ที่ควรรู้
| Keyword | หมวดหมู่ | คำอธิบายสั้น |
|---|---|---|
| virtual | Polymorphism | อนุญาตให้ override |
| override | Polymorphism | เขียนทับ virtual/abstract method |
| new (hiding) | Polymorphism | ซ่อน base member (shadowing) |
| static | Member | เป็นของ Type ไม่ใช่ Instance |
| readonly | Field | กำหนดค่าได้แค่ declaration/constructor |
| const | Field | Compile-time constant |
| ref/out/in | Parameter | Pass by reference variants |
| async/await | Async | Asynchronous programming |
| yield return | Iterator | สร้าง IEnumerable แบบ Lazy |
| using | Resource/Import | IDisposable cleanup / namespace import |
| lock | Threading | Monitor synchronization |
| unsafe | Memory | Pointer operations |
| volatile | Threading | ป้องกัน compiler optimization |
| partial | Type | แยก definition หลายไฟล์ |
| record | Type (C# 9) | Immutable data type with value equality |
| init | Property (C# 9) | Set ได้แค่ตอน init/constructor |