C และ C# เป็นภาษาที่ต่างกันหมด แม้ชื่อจะคล้ายกัน C เป็นภาษาโครงสร้าง (Procedural) เก่า C# เป็นภาษา Object-Oriented ทันสมัยบน .NET
ตารางเปรียบเทียบ
| ด้าน | C | C# |
|---|---|---|
| Paradigm | Structured / Procedural | Object-Oriented, Multi-paradigm |
| Memory Management | Manual (malloc/free) | Automatic (Garbage Collection - CLR) |
| Garbage Collection | ไม่มี | มี (Generational GC) |
| Platform | Cross-platform (Compile per platform) | Windows (Framework) / Cross-platform (.NET Core/5+) |
| Type Safety | Weak (Pointer arithmetic) | Strong (Type-safe by default) |
| Exception Handling | ไม่มี (ใช้ Error Codes) | มี (try/catch/finally) |
| Generics | ไม่มี | มี (Generics, Constraints) |
| LINQ / Lambda | ไม่มี | มี |
| Async/Await | ไม่มี | มี |
| Standard Library | เล็ก (C Standard Library) | ใหญ่ (BCL - Base Class Library) |
รายละเอียดเพิ่มเติม
- C: พัฒนาโดย Dennis Ritchie ปี 1972 สำหรับ UNIX ใช้ Pointer โดยตรง ใกล้ Hardware เร็ว เหมาะ Embedded/OS
- C#: พัฒนาโดย Microsoft ปี 2000 บน .NET Managed Code รันบน CLR มี Reflection, Attributes, Delegates, Events
รหัส Hello World เปรียบเทียบ
// C
#include <stdio.h>
int main() { printf("Hello World\n"); return 0; }
// C#
using System;
class Program { static void Main() { Console.WriteLine("Hello World"); } }
สรุป: C ใช้สำหรับ System Programming, Embedded, Performance-critical | C# ใช้สำหรับ Application Development, Web, Desktop, Mobile, Cloud