ใน C# คลาส (Class) ประกอบด้วยสมาชิก (Members) 2 ประเภทหลักคือ Fields/Properties (Class Members/Attributes) และ Methods (Class Methods/Behaviors) ทั้งคู่มีบทบาทและลักษณะการทำงานที่แตกต่างกันอย่างชัดเจน
1. Class Methods (เมธอด / ฟังก์ชันสมาชิก)
เมธอดคือบล็อกโค้ดที่ทำหน้าที่ กระทำ (Behavior) หรือ คำนวณ ใส่ได้ใน Class Definition มี Signature ประกอบด้วย Access Modifier, Return Type, Name และ Parameters
ตัวอย่าง: เมธอด Setter และ Getter
public void SetLength(double len) { length = len; }
public void SetBreadth(double bre) { breadth = bre; }
public double GetVolume() { return length * breadth * height; }
ตัวอย่างโค้ดสมบูรณ์: Class Box ใช้ Methods จัดการข้อมูล
using System;
namespace BoxApplication
{
class Box
{
// Private Fields (Class Members/Attributes)
private double length;
private double breadth;
private double height;
// Public Methods (Behaviors)
public void SetLength(double len) => length = len;
public void SetBreadth(double bre) => breadth = bre;
public void SetHeight(double hei) => height = hei;
public double GetVolume() => length * breadth * height;
}
class Boxtester
{
static void Main(string[] args)
{
Box box1 = new Box();
Box box2 = new Box();
double volume;
// ใช้ Methods ตั้งค่า
box1.SetLength(8.0);
box1.SetBreadth(9.0);
box1.SetHeight(7.0);
box2.SetLength(18.0);
box2.SetBreadth(20.0);
box2.SetHeight(17.0);
// ใช้ Method คำนวณ
volume = box1.GetVolume();
Console.WriteLine("Volume of Box1: {0}", volume); // 504
volume = box2.GetVolume();
Console.WriteLine("Volume of Box2: {0}", volume); // 6120
Console.ReadKey();
}
}
}
2. Class Members (Fields / Properties / Attributes)
Class Members คือ ข้อมูล (Data/State) ที่เก็บอยู่ในแต่ละ Instance (Object) มักจะเป็น private เพื่อ Encapsulation (การห่อหุ้ม) และเข้าถึงผ่าน Public Methods/Properties
ตัวอย่าง: Member Variables ใน Class Rectangle
// Member Variables (Fields)
private double length;
private double width;
// Methods ใช้งาน Fields
public void AcceptDetails() { length = 10; width = 14; }
public double GetArea() => length * width;
ตัวอย่างโค้ดสมบูรณ์
using System;
namespace RectangleApplication
{
class Rectangle
{
// Member Variables (Fields) - Private สำหรับ Encapsulation
private double length;
private double width;
// Methods
public void AcceptDetails()
{
length = 10;
width = 14;
}
public double GetArea() => length * width;
public void Display()
{
Console.WriteLine("Length: {0}", length);
Console.WriteLine("Width: {0}", width);
Console.WriteLine("Area: {0}", GetArea());
}
}
class ExecuteRectangle
{
static void Main(string[] args)
{
Rectangle r = new Rectangle();
r.AcceptDetails();
r.Display();
Console.ReadLine();
}
}
}
ผลลัพธ์
Length: 10 Width: 14 Area: 140
ตารางเปรียบเทียบสรุป
| ลักษณะ | Class Methods (เมธอด) | Class Members / Fields (สมาชิก/ฟิลด์) |
|---|---|---|
| บทบาท | พฤติกรรม (Behavior) - "ทำอะไร" | สถานะ (State) - "มีอะไร" |
| Syntax | ReturnType Name(Params) { body } |
Type Name [= initial]; |
| Access | เรียกผ่าน Object: obj.Method() |
เข้าถึงโดยตรง (ถ้า public) หรือผ่าน Property/Method |
| Encapsulation | เป็น Interface สำหรับเข้าถึงข้อมูล | ควรเป็น private ซ่อนรายละเอียดภายใน |
| Memory | ร่วมกันทุก Instance (Code Segment) | แต่ละ Instance มี Copy ของตัวเอง (Heap) |
| ตัวอย่าง | GetVolume(), SetLength(), CalculateTax() | length, width, _accountBalance, _name |
Best Practice: Properties แทน Public Fields
ใน C# สมัยใหม่ ควรใช้ Auto-Implemented Properties แทน Public Fields:
// แทนที่จะเขียน private field + public methods
private double _length;
public double Length { get => _length; set => _length = value; }
// หรือสั้นกว่า (Auto-property)
public double Length { get; set; }
// Read-only property (คำนวณจาก fields อื่น)
public double Area => Length * Width;
สรุปใจความสำคัญ
- Methods = ฟังก์ชันที่ทำงาน/คำนวณ/กระทำ
- Members (Fields) = ตัวแปรที่เก็บข้อมูลของแต่ละ Object
- Encapsulation = ทำ Fields เป็น private ให้เข้าถึงผ่าน Methods/Properties เท่านั้น
- Properties = Syntax สวยงามสำหรับ Get/Set Fields (รองรับ Validation, Lazy Loading, Notification)