Initialization (Khởi tạo giá trị)
Gán giá trị khi khai báo biến:
int val = 50;
Mảng cần new:
int[] arr = new int[] { 1, 2, 3 };
Instantiation (Khởi tạo thể hiện/instance)
Tạo object mới từ class bằng new:
Student s1 = new Student(); Student s2 = new Student();
So sánh
| Khía cạnh | Initialization | Instantiation |
|---|---|---|
| Đối tượng | Biến (value type, reference) | Object (reference type/class) |
| Từ khóa | Có thể không cần new | Luôn cần new (trừ singleton/pool) |
| Mảng | Cần new (trừ array initializer) | Là một dạng instantiation |
| Struct | Khởi tạo giá trị | new gọi constructor (có thể bỏ qua) |
Ví dụ kết hợp:
// Declaration + Instantiation + Initialization
List<int> list = new List<int>() { 1, 2, 3 };
// ^declaration ^instantiation ^initialization (collection initializer)