C# (ซีชาร์ป) คืออะไร?
C# เป็นภาษาโปรแกรมมิ่งสมัยใหม่ ทั่วไป (General-Purpose) เชิงวัตถุ (Object-Oriented) พัฒนาโดย Microsoft ภายใต้โครงการ .NET
ลักษณะสำคัญ
- Modern: รองรับฟีเจอร์สมัยใหม่ (LINQ, Async/Await, Pattern Matching, Records)
- Type-Safe: Static Typing ตรวจสอบ Type ตอน Compile
- Managed: รันบน CLR (Common Language Runtime) - Garbage Collection, Memory Safety
- Cross-Platform: .NET Core / .NET 5+ รันบน Windows, Linux, macOS
- Multi-Paradigm: OOP, Functional, Generic, Component-Oriented
Common Language Infrastructure (CLI)
C# ออกแบบสำหรับ CLI ซึ่งประกอบด้วย:
- Executable Code: CIL (Common Intermediate Language) - Bytecode
- Runtime Environment: CLR - JIT Compilation, GC, Security, Interop
- รองรับภาษาอื่นๆ: VB.NET, F#, C++/CLI บน Platform เดียวกัน
คุณสมบัติเด่นของ C# (Features)
| Feature | คำอธิบาย |
|---|---|
| Boolean Conditions | เงื่อนไข Boolean จริง (true/false) ไม่ใช่ 0/1 |
| Automatic Garbage Collection | จัดการหน่วยความจำอัตโนมัติ ไม่ต้อง Free Manual |
| Standard Library (BCL) | Framework Class Library กว่า 10,000+ Types |
| Assembly Versioning | จัดการ Versioning แบบ Strong-Named Assemblies |
| Properties & Events | Encapsulation ด้วย Properties, Event-Driven Programming |
| Delegates & Events | Type-Safe Function Pointers, Event Pattern |
| Generics | Type-Safe Reusable Code (List<T>, Dictionary<K,V>) |
| Indexers | Array-like Syntax สำหรับ Custom Collections |
| Conditional Compilation | #if DEBUG etc. สำหรับ Build Configurations |
| Multithreading | Thread, Task, Async/Await, Parallel, PLINQ |
| LINQ & Lambda | Query Syntax, Method Syntax, Lambda Expressions |
| Windows Integration | WPF, WinForms, UWP, MAUI, WinUI |
Hello World ใน C# (Modern .NET 6+)
// Top-level Statements (C# 9+)
using System;
Console.WriteLine("Hello, World!");
// หรือแบบ Classic
namespace HelloWorld {
class Program {
static void Main(string[] args) {
Console.WriteLine("Hello, World!");
}
}
}
เวอร์ชันสำคัญของ C#
| Version | Year | Key Features |
|---|---|---|
| C# 1.0 | 2002 | Basic OOP, Delegates, Events |
| C# 2.0 | 2005 | Generics, Iterators, Anonymous Methods, Nullable Types |
| C# 3.0 | 2007 | LINQ, Lambda, Extension Methods, Var, Auto-Properties |
| C# 4.0 | 2010 | Dynamic, Named/Optional Args, Covariance/Contravariance |
| C# 5.0 | 2012 | Async/Await, Caller Info Attributes |
| C# 6.0 | 2015 | String Interpolation, nameof, Null-Conditional, Expression-bodied |
| C# 7.0 | 2017 | Tuples, Pattern Matching, Ref Returns, Local Functions |
| C# 8.0 | 2019 | Nullable Reference Types, Default Interface Methods, Ranges |
| C# 9.0 | 2020 | Records, Init-only Properties, Top-level Statements |
| C# 10.0 | 2021 | Global Using, File-scoped Namespaces, Record Structs |
| C# 11.0 | 2022 | Raw String Literals, Generic Math, Required Members |
| C# 12.0 | 2023 | Primary Constructors, Collection Expressions, Alias Any Type |
การประยุกต์ใช้งาน C#
- Web: ASP.NET Core, Blazor, Web API, Minimal APIs
- Desktop: WPF, WinForms, WinUI 3, MAUI
- Mobile: .NET MAUI, Xamarin (Legacy)
- Cloud: Azure Functions, AWS Lambda, Container Apps
- Game: Unity (C# Scripting), Godot (C#)
- IoT/Embedded: .NET nanoFramework, Meadow
- AI/ML: ML.NET, TensorFlow.NET, ONNX Runtime