โอเค 😎 เรามา รวบรวมรหัส Error ของ Visual Studio / C# พร้อม คำอธิบายและวิธีแก้ไข แบบครอบคลุมให้คุณเป็น Reference ใช้เวลาโค้ดจริง
1️⃣ Common C# Compiler Errors
| รหัส Error | คำอธิบาย | วิธีแก้ไข |
|---|---|---|
| CS1002 | Missing semicolon (;) |
ตรวจสอบบรรทัดก่อนหน้า และเพิ่ม ; ให้ถูกต้อง |
| CS1003 | Syntax error: unexpected token | ตรวจสอบ syntax ของ statement เช่น if, for หรือ expression |
| CS1026 | Missing closing parenthesis ) |
ตรวจสอบวงเล็บให้ครบ เช่น Console.WriteLine("Hello" |
| CS0103 | The name 'X' does not exist in the current context | ตัวแปร/Method ยังไม่ได้ประกาศ, ตรวจสอบการสะกดชื่อและ scope |
| CS0165 | Use of unassigned local variable | ประกาศและกำหนดค่าเริ่มต้นให้ตัวแปร |
| CS1501 | No overload for method matches arguments | ตรวจสอบจำนวนและชนิด parameter ที่เรียกใช้ method |
| CS0029 | Cannot implicitly convert type 'X' to 'Y' | ใช้ type casting หรือแก้ type ให้ตรงกัน |
| CS0266 | Cannot implicitly convert type 'X' to 'Y'. Explicit conversion exists | ใช้ (Y)X cast หรือเปลี่ยน type ให้ตรง |
| CS0246 | Type or namespace name 'X' could not be found | ตรวจสอบ using directives หรือ NuGet package ที่ขาด |
| CS1701 | Assembly reference mismatch | ตรวจสอบ version ของ assembly ให้ตรงกับโปรเจกต์ |
2️⃣ Runtime Errors / Exceptions
| Exception | คำอธิบาย | วิธีแก้ไข |
|---|---|---|
| NullReferenceException | พยายามเรียก member ของ object ที่เป็น null | ตรวจสอบ object ก่อนใช้งาน: if(obj != null) |
| IndexOutOfRangeException | Array / List index เกินขอบเขต | ตรวจสอบ index ว่า < array.Length |
| InvalidOperationException | Operation ไม่เหมาะสมกับ state ปัจจุบัน | ตรวจสอบ logic ของ method ก่อนเรียกใช้งาน |
| ArgumentNullException | Parameter เป็น null แต่ method ไม่รับ | ตรวจสอบค่าที่ส่งไป method และใช้ ?? หรือ default value |
| FormatException | พยายาม parse string เป็น number/DateTime ผิด format | ตรวจสอบ input ก่อนใช้ int.TryParse หรือ DateTime.TryParse |
| OverflowException | Arithmetic overflow เช่น int มากกว่า 2,147,483,647 | ใช้ชนิดตัวเลขใหญ่กว่า หรือ checked/unchecked |
| DivideByZeroException | หารด้วย 0 | ตรวจสอบ denominator ก่อนทำการหาร |
| FileNotFoundException | File ไม่พบ | ตรวจสอบ path และว่าไฟล์มีจริงหรือไม่ |
| UnauthorizedAccessException | ไม่มีสิทธิ์เข้าถึงไฟล์หรือ resource | ตรวจสอบ permission หรือรัน Visual Studio เป็น admin |
| IOException | เกิดข้อผิดพลาดในการอ่าน/เขียนไฟล์ | ตรวจสอบ path, permission, หรือ file lock |
3️⃣ Common .NET / Visual Studio Build Errors
| รหัส Error | คำอธิบาย | วิธีแก้ไข |
|---|---|---|
| MSB3021 | Copying file failed | ตรวจสอบว่าไฟล์เปิดอยู่, permission, หรือ path ถูกต้อง |
| MSB3270 | Reference to assembly conflicts with another assembly | ลบ assembly ซ้ำหรือปรับ version ให้ตรง |
| NU1605 | Package downgrade detected | ปรับเวอร์ชัน NuGet ให้ตรง |
| NU1107 | Version conflict for dependency | ใช้ PackageReference version ล่าสุดหรือแก้ dependencies |
| CS1705 | Assembly has strong name mismatch | ตรวจสอบ reference ของ library ให้ตรงกับ target framework |
4️⃣ Tips แก้ไข Error
-
อ่าน ข้อความ Error ให้ละเอียด
-
ตรวจสอบ ไฟล์และบรรทัดที่แจ้ง
-
ตรวจสอบ NuGet packages หรือ using directives
-
ตรวจสอบ type และ parameter ให้ตรง
-
ใช้ Try/Catch สำหรับ runtime exception
-
ค้นหา รหัส Error บน Microsoft Docs หรือ StackOverflow