ได้เลย 😎 มาสรุป Class ที่ Built-in ใน C# ที่อยู่ใน Assembly หลัก ๆ พร้อมคำอธิบายและตัวอย่างโค้ดให้เข้าใจง่าย
1️⃣ Assembly คืออะไร?
-
Assembly = ไฟล์ .dll หรือ .exe ที่รวม Code / Metadata / Manifest
-
C# มี Built-in Assemblies หลัก เช่น:
-
mscorlib.dll
→ มี Object, String, Int32, Exception, Collections -
System.dll
→ มี File, Directory, Console, Math -
System.Core.dll
→ LINQ, Tasks -
System.Collections.dll
→ Collection Generic / Non-Generic
-
2️⃣ ตัวอย่าง Class Built-in ตาม Assembly
Assembly | Class | คำอธิบาย | ตัวอย่างโค้ด |
---|---|---|---|
mscorlib |
System.Object |
คลาสแม่ของทุก class | object obj = new object(); |
mscorlib |
System.String |
ข้อความ, immutable | string s = "Hello"; Console.WriteLine(s.Length); |
mscorlib |
System.Int32 |
จำนวนเต็ม 32-bit | int x = 10; |
mscorlib |
System.Exception |
Base class ของ Error | throw new Exception("Error"); |
System |
System.Console |
อ่าน/เขียน Console | Console.WriteLine("Hello"); |
System |
System.Math |
ฟังก์ชันคณิตศาสตร์ | double pi = Math.PI; |
System.IO |
System.IO.File |
จัดการไฟล์ | File.WriteAllText("a.txt","Hello"); |
System.IO |
System.IO.Directory |
จัดการโฟลเดอร์ | Directory.CreateDirectory("Test"); |
System.Collections |
System.Collections.ArrayList |
List แบบ non-generic | ArrayList list = new ArrayList(); list.Add(1); |
System.Collections.Generic |
System.Collections.Generic.List<T> |
List แบบ generic | List<int> numbers = new List<int>{1,2,3}; |
System.Collections.Generic |
Dictionary<TKey,TValue> |
Key-Value Collection | var dic = new Dictionary<int,string>{{1,"A"}}; |
System.Threading.Tasks |
System.Threading.Tasks.Task |
Async / Parallel | await Task.Run(()=>Console.WriteLine("Hi")); |
System.Linq |
System.Linq.Enumerable |
LINQ Method | var evens = new List<int>{1,2,3}.Where(x=>x%2==0); |
System.DateTime |
System.DateTime |
วันเวลา | DateTime now = DateTime.Now; |
System.TimeSpan |
System.TimeSpan |
ระยะเวลา | TimeSpan ts = new TimeSpan(1,30,0); |
3️⃣ ตัวอย่างการใช้งานรวม
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
class Program
{
static async Task Main()
{
// String
string name = "Jen";
Console.WriteLine(name.ToUpper());
// List
List<int> numbers = new List<int> {1, 2, 3, 4};
var even = numbers.Where(n => n % 2 == 0).ToList();
Console.WriteLine(string.Join(",", even));
// File IO
File.WriteAllText("test.txt", "Hello World");
Console.WriteLine(File.ReadAllText("test.txt"));
// Task / Async
await Task.Run(() => Console.WriteLine("Async Task"));
// DateTime
Console.WriteLine(DateTime.Now);
}
}
4️⃣ Tips
-
ถ้าใช้ .NET 6+ / .NET 7+, ทุก class พื้นฐานส่วนใหญ่ อยู่ใน
System.Private.CoreLib.dll
-
LINQ / Collections / IO จะเป็น แยก Assembly (
System.Linq.dll
,System.Collections.dll
,System.IO.FileSystem.dll
) -
เราสามารถ ดู Assembly ของ Class ด้วย Reflection
Console.WriteLine(typeof(string).Assembly); // System.Private.CoreLib
Console.WriteLine(typeof(List<>).Assembly); // System.Collections.dll
💡 สรุป:
-
Assembly คือกลุ่ม Class / Code ที่คอมไพล์เป็น .dll
-
Class Built-in → String, Int32, Object, List, Dictionary, File, Task, LINQ
-
รู้ Assembly จะช่วยให้เข้าใจ การ Reference / Import / ใช้งาน Library