Access Modifiers 6 ระดับ
ควบคุม Visibility/Scope ของ Type และ Member
| Modifier | Scope | เข้าถึงได้จาก |
public | Everywhere | Assembly ใดๆ, Derived Class ใดๆ |
private | Containing Type Only | Class/Struct เดียวกันเท่านั้น |
protected | Containing + Derived | Class เดียวกัน + Derived Class (ทุก Assembly) |
internal | Current Assembly | Class ใดๆ ใน Assembly เดียว |
protected internal | Union: Protected OR Internal | Same Assembly OR Derived Class (Any Assembly) |
private protected | Intersection: Protected AND Internal | Same Assembly AND Derived Class (C# 7.2+) |
ตัวอย่างแต่ละระดับ
public - เข้าถึงได้ทุกที่
public class Person
{
public string Name = "Mark"; // Public Field
}
class Program
{
static void Main()
{
var p = new Person();
Console.WriteLine(p.Name); // ✅ OK
}
}
private - เข้าถึงได้ใน Class เดียว
public class Person
{
private string _ssn = "123-45-6789"; // Private Field
public string GetSsn() => _ssn; // Access ผ่าน Method
}
class Program
{
static void Main()
{
var p = new Person();
// Console.WriteLine(p._ssn); // ❌ Error: Inaccessible
Console.WriteLine(p.GetSsn()); // ✅ OK
}
}
protected - Class + Derived Classes
public class Person
{
protected string Name = "Mark";
}
public class Employee : Person
{
public void Print() => Console.WriteLine(Name); // ✅ OK (Derived)
}
class Program
{
static void Main()
{
var e = new Employee();
e.Print(); // Mark
var p = new Person();
// Console.WriteLine(p.Name); // ❌ Error: Not Derived
}
}
internal - Assembly เดียว
// Project 1 (MyApplication)
public class Person
{
internal string Name = "Mark"; // Internal
}
// Project 2 (References MyApplication)
public class OtherClass
{
public void Test()
{
var p = new Person();
// Console.WriteLine(p.Name); // ❌ Error: Internal
}
}
protected internal - Protected OR Internal
// Project 1
public class Person
{
protected internal string Name = "Mark";
}
// Project 2 (Derived)
public class Manager : Person
{
public void Print() => Console.WriteLine(Name); // ✅ OK (Protected)
}
// Project 2 (Not Derived)
public class Other
{
public void Test()
{
var p = new Person();
// Console.WriteLine(p.Name); // ❌ Error (Not Derived, Not Same Assembly)
}
}
private protected - Protected AND Internal (C# 7.2+)
// Project 1
public class Person
{
private protected string Name = "Mark"; // Private Protected
}
public class Employee : Person
{
public void Print() => Console.WriteLine(Name); // ✅ OK (Same Assembly + Derived)
}
// Project 2 (Derived but Different Assembly)
public class Manager : Person
{
public void Print() => Console.WriteLine(Name); // ❌ Error (Different Assembly)
}
Default Access Modifiers
| Context | Default |
| Class/Struct (Namespace Level) | internal |
| Class/Struct (Nested) | private |
| Interface Members | public (Implicit) |
| Enum Members | public |
| Delegate | internal (Namespace) / private (Nested) |
Best Practices
- ✅ เริ่มจาก
private เปิดขึ้นเรื่อยๆ ตามความจำเป็น (Principle of Least Privilege)
- ✅ Field:
private + Property Public (Encapsulation)
- ✅ Internal สำหรับ Implementation Details ภายใน Assembly
- ✅ Protected สำหรับ Base Class Members ที่ Derived ต้องใช้
- ❌ หลีกเลี่ยง
public Field (ใช้ Property แทน)