ได้เลย 😎 มาสรุป Class / Type Build-in ที่ใช้บ่อยใน C# พร้อมคำอธิบายสั้น ๆ เพื่อให้เข้าใจง่าย
1️⃣ System.Object
-
คำอธิบาย: คลาสแม่ของทุก class ใน C#
-
ทุก class จะสืบทอด
Object
โดยอัตโนมัติ -
Method ที่สำคัญ:
ToString()
,GetHashCode()
,Equals()
,GetType()
object obj = new object();
Console.WriteLine(obj.ToString());
2️⃣ System.String
-
คำอธิบาย: เก็บข้อความ / ตัวอักษร (immutable)
-
Method ที่สำคัญ:
Length
,Substring
,Contains
,Replace
,Split
string name = "Jen";
Console.WriteLine(name.Length); // 3
Console.WriteLine(name.Contains("J")); // True
3️⃣ System.Int32 / Int64 / Double / Decimal / Boolean
-
คำอธิบาย: ประเภทตัวเลขและค่าความจริง
-
int
→ 32-bit integer -
long
→ 64-bit integer -
double
→ floating point -
decimal
→ จำนวนทศนิยมแม่นยำสูง (ใช้เงิน) -
bool
→ true / false
int age = 25;
double pi = 3.14159;
bool isActive = true;
4️⃣ System.DateTime
-
คำอธิบาย: จัดการวันและเวลา
-
Method สำคัญ:
Now
,Today
,AddDays
,AddHours
,ToString()
DateTime now = DateTime.Now;
Console.WriteLine(now.AddDays(1)); // วันพรุ่งนี้
5️⃣ System.TimeSpan
-
คำอธิบาย: ตัวแทนระยะเวลา (เวลาแตกต่างระหว่าง DateTime)
-
Method สำคัญ:
TotalSeconds
,TotalMinutes
TimeSpan duration = new TimeSpan(2, 30, 0); // 2 ชั่วโมง 30 นาที
Console.WriteLine(duration.TotalMinutes); // 150
6️⃣ System.Collections.Generic.List
-
คำอธิบาย: รายการข้อมูลแบบ dynamic array
-
Method สำคัญ:
Add
,Remove
,Count
,Contains
,Sort
List<string> fruits = new List<string>();
fruits.Add("Apple");
fruits.Add("Banana");
Console.WriteLine(fruits.Count); // 2
7️⃣ System.Collections.Generic.Dictionary<TKey, TValue>
-
คำอธิบาย: เก็บคู่ Key-Value
-
Method สำคัญ:
Add
,ContainsKey
,TryGetValue
Dictionary<int, string> users = new Dictionary<int, string>();
users.Add(1, "Jen");
users.Add(2, "Mai");
Console.WriteLine(users[1]); // Jen
8️⃣ System.Collections.Generic.HashSet
-
คำอธิบาย: เก็บข้อมูลไม่ซ้ำ (Unique)
-
Method สำคัญ:
Add
,Remove
,Contains
HashSet<int> numbers = new HashSet<int>();
numbers.Add(1);
numbers.Add(1); // ซ้ำ ไม่เพิ่ม
Console.WriteLine(numbers.Count); // 1
9️⃣ System.Collections.Generic.Queue / Stack
-
Queue: First-In-First-Out (FIFO)
-
Stack: Last-In-First-Out (LIFO)
Queue<string> queue = new Queue<string>();
queue.Enqueue("A");
queue.Enqueue("B");
Console.WriteLine(queue.Dequeue()); // A
Stack<string> stack = new Stack<string>();
stack.Push("X");
stack.Push("Y");
Console.WriteLine(stack.Pop()); // Y
🔟 System.Exception
-
คำอธิบาย: Base class ของ Exception ใน C#
-
ใช้สำหรับ จับและโยนข้อผิดพลาด
-
Method สำคัญ:
Message
,StackTrace
try
{
throw new Exception("Something went wrong!");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
11️⃣ System.IO.File / Directory / Path
-
คำอธิบาย: ใช้จัดการไฟล์และโฟลเดอร์
-
ตัวอย่าง Method:
File.ReadAllText
,File.WriteAllText
,Directory.CreateDirectory
File.WriteAllText("test.txt", "Hello World");
string text = File.ReadAllText("test.txt");
Console.WriteLine(text);
12️⃣ System.Threading.Tasks.Task
-
คำอธิบาย: ใช้ทำงานแบบ Asynchronous / Parallel
-
Method สำคัญ:
Run
,Delay
,WhenAll
,Await
await Task.Run(() => Console.WriteLine("Hello from Task!"));
13️⃣ System.Linq.Enumerable / Queryable
-
คำอธิบาย: ใช้ LINQ สำหรับ query collection
-
Method สำคัญ:
Where
,Select
,FirstOrDefault
,OrderBy
var numbers = new List<int> { 1, 2, 3, 4, 5 };
var evenNumbers = numbers.Where(n => n % 2 == 0).ToList();
Console.WriteLine(string.Join(",", evenNumbers)); // 2,4
💡 สรุป:
-
พื้นฐาน: Object, String, Int32, Boolean, DateTime
-
Collection: List, Dictionary, HashSet, Queue, Stack
-
Error Handling: Exception
-
File IO: File, Directory
-
Async: Task
-
Query: LINQ
ถ้าคุณอยาก ฉันสามารถทำ ตาราง Cheat Sheet ของ Class Built-in + Method ยอดนิยม + ตัวอย่างสั้น ๆ ให้ใช้เป็น Reference แบบเร็ว ๆ เลย
คุณอยากให้ทำไหม?