method คือฟังก์ชันที่กำหนดไว้ใน class C# รองรับฟีเจอร์ parameter ที่หลากหลาย ได้แก่ optional/named argument, modifier ref/out/in, อาเรย์ params และ (หลาย method ที่มีชื่อเดียวกันแต่ parameter ต่างกัน)
public int Add(int a, int b) => a + b; // expression-bodied (concise)
public string Greet(string name, string greeting = "Hello") // default parameter
{
return $"{greeting}, {name}";
}
Greet("Ann"); // "Hello, Ann"
Greet("Ann", greeting: "Hi"); // named argument
method ระบุ return type, ชื่อ และ parameter ส่วน default parameter ทำให้ argument เป็น optional และ named argument ช่วยให้ส่งค่าตามชื่อได้ (ชัดเจนกว่า และข้าม optional ได้)
// ref — pass by reference (method can modify the caller's variable)
void Increment(ref int x) { x++; }
int n = 5; Increment(ref n); // n is now 6
// out — for returning multiple values (must be assigned in the method)
bool TryParse(string s, out int result) { result = ...; return true; }
if (int.TryParse("42", out int value)) { } // the common TryX pattern
// in — pass by reference but READ-ONLY (performance for large structs)
void Process(in LargeStruct data) { }
out ถูกใช้อย่างแพร่หลายใน TryParse pattern (คืนค่าธงบ่งบอกความสำเร็จพร้อมผลลัพธ์อย่างปลอดภัย), ref อนุญาตให้แก้ไขตัวแปรของผู้เรียก ส่วน in ส่ง struct ขนาดใหญ่อย่างมีประสิทธิภาพโดยไม่ต้องคัดลอก
// params — variable number of arguments
int Sum(params int[] numbers) => numbers.Sum();
Sum(1, 2, 3, 4); // any number
// OVERLOADING — same name, different parameters
void Print(int x) { }
void Print(string s) { }
void Print(int x, int y) { }
Print(5); // calls the int version — resolved by argument types
overloading ช่วยให้หลาย method ใช้ชื่อร่วมกันได้ โดยแยกแยะด้วยชนิด/จำนวนของ parameter คอมไพเลอร์เลือกตัวที่ตรงกัน ส่วน params รับ argument ในจำนวนที่ไม่จำกัด
method เป็นหน่วยสร้างพื้นฐานของพฤติกรรมใน C# และการเข้าใจฟีเจอร์ parameter ที่หลากหลายมีความสำคัญต่อการเขียนโค้ดที่ยืดหยุ่นและสื่อความหมายได้ดี
การรู้วิธีกำหนด method (รวมถึงไวยากรณ์ expression-bodied ที่กระชับ) การใช้ default และ named parameter (สำหรับ argument ที่เป็น optional และการเรียกที่อ่านง่าย) และการใช้ method overloading (ชื่อเดียวกัน parameter ต่างกัน สำหรับ API ที่ใช้งานง่ายและรองรับชนิด argument ที่ต่างกัน) เป็นความรู้ที่ใช้ทุกวัน
parameter modifier สำคัญเป็นพิเศษที่ต้องเข้าใจ: out ถูกใช้อย่างแพร่หลายใน TryParse pattern ที่พบบ่อย (int.TryParse(s, out var result) คืนค่าธงบ่งบอกความสำเร็จและผลลัพธ์อย่างปลอดภัยโดยไม่ใช้ exception ซึ่งเป็น pattern ที่พบบ่อยและเป็นสำนวนของ C#), ref ช่วยให้แก้ไขตัวแปรของผู้เรียกได้ ส่วน in ส่ง struct ขนาดใหญ่แบบ by reference เพื่อประสิทธิภาพ
การเข้าใจสิ่งเหล่านี้ — method, ฟีเจอร์ parameter (optional/named/params), overloading สำหรับ API ที่ยืดหยุ่น และ modifier ref/out/in (โดยเฉพาะสำนวน TryParse ที่ใช้ out) — เป็นพื้นฐานทั้งสำหรับการเขียน method ใน C# และการใช้ .NET API จำนวนมากที่พึ่งพาฟีเจอร์เหล่านี้
เนื่องจาก method และ parameter เป็นศูนย์กลางของโค้ด C# ทั้งหมด และเนื่องจากฟีเจอร์อย่าง overloading, optional parameter และ TryX pattern ปรากฏอยู่ตลอด การเชี่ยวชาญสิ่งเหล่านี้จึงเป็นความรู้หลักที่ต้องรู้สำหรับการพัฒนา C# อย่างมีประสิทธิภาพ