Structure (Struct) ใน C#
Struct เป็น Value Type Data Type ช่วยให้ตัวแปรเดียวจัดเก็บข้อมูลที่เกี่ยวข้องหลายชนิดข้อมูล (Data Types) ได้ ใช้คีย์เวิร์ด struct
การประกาศ Structure
struct Books {
public string Title;
public string Author;
public string Subject;
public int Id;
}
ความแตกต่าง Struct vs Class
| คุณสมบัติ | Struct (Value Type) | Class (Reference Type) |
|---|---|---|
| จัดเก็บใน | Stack | Heap |
| Inheritance | ไม่สืบทอด Class ได้ (แต่ Implement Interface ได้) | สืบทอดได้ |
| Constructor | ไม่มี Parameterless Constructor (Implicit) | มี Parameterless Constructor |
| Null | ไม่สามารถเป็น Null ได้ (Nullable Struct ได้) | สามารถเป็น Null ได้ |
| ประสิทธิภาพ | ดีกว่าสำหรับข้อมูลเล็ก | เหมาะสำหรับข้อมูลใหญ่/ซับซ้อน |
| การส่งผ่าน | Pass by Value (Copy) | Pass by Reference |
ตัวอย่างการใช้งาน Struct
using System;
struct Books {
public string Title;
public string Author;
public int Id;
}
public class TestStructure {
public static void Main(string[] args) {
Books book1; // ไม่ต้อง new (แต่แนะนำให้ new เพื่อความชัดเจน)
book1.Title = "PHP IN 7 Days";
book1.Author = "Jacob Dawson";
book1.Id = 34;
Console.WriteLine("Book 1 title : {0}", book1.Title);
Console.WriteLine("Book 1 author : {0}", book1.Author);
Console.WriteLine("Book 1 book_id : {0}", book1.Id);
// ใช้ Constructor (C# 10+ 可以有 parameterless)
Books book2 = new Books {
Title = "C# in Depth",
Author = "Jon Skeet",
Id = 101
};
Console.WriteLine("\nBook 2: {0} by {1}", book2.Title, book2.Author);
Console.ReadKey();
}
}
Struct Members (สมาชิกของ Struct)
Struct สามารถมี Members ต่อไปนี้ได้:
- Fields - ตัวแปรสมาชิก
- Properties - คุณสมบัติ (Get/Set)
- Methods - เมธอด
- Constructors - Constructor ที่มีพารามิเตอร์ (C# 10+ มี Parameterless ได้)
- Events - เหตุการณ์
- Indexers - Indexer
- Operators - Operator Overloading
- Nested Types - Struct/Class/Enum/Delegate ภายใน
ตัวอย่าง Struct แบบสมบูรณ์
using System;
public struct Point {
// Fields
public int X;
public int Y;
// Constructor
public Point(int x, int y) {
X = x;
Y = y;
}
// Method
public double DistanceTo(Point other) {
int dx = X - other.X;
int dy = Y - other.Y;
return Math.Sqrt(dx * dx + dy * dy);
}
// Operator Overloading
public static Point operator +(Point a, Point b) {
return new Point(a.X + b.X, a.Y + b.Y);
}
// Override ToString
public override string ToString() {
return $"({X}, {Y})";
}
}
class Program {
static void Main() {
Point p1 = new Point(3, 4);
Point p2 = new Point(6, 8);
Point p3 = p1 + p2; // Operator Overloading
Console.WriteLine($"P1: {p1}");
Console.WriteLine($"P2: {p2}");
Console.WriteLine($"P1 + P2 = {p3}");
Console.WriteLine($"Distance: {p1.DistanceTo(p2)}"); // 5
}
}
เมื่อไหร่ควรใช้ Struct?
- ข้อมูลเล็ก (Small Data) - น้อยกว่า 16 bytes
- Immutable (ค่าคงที่หลังสร้าง)
- สร้าง Instance จำนวนมาก (Performance - No GC Pressure)
- Primitive-like Types (Point, DateTime, Decimal, Guid)
- ไม่ต้องการ Inheritance
เมื่อไหร่ควรใช้ Class?
- ข้อมูลใหญ่/ซับซ้อน
- Mutable (เปลี่ยนแปลงได้)
- ต้องการ Inheritance/Polymorphism
- Reference Semantics (Share State)
- Business Objects, Entities, Services