Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

Access Modifiers ใน C# มีกี่แบบ ต่างกันอย่างไร

Access Modifiers 6 ระดับ

ควบคุม Visibility/Scope ของ Type และ Member

ModifierScopeเข้าถึงได้จาก
publicEverywhereAssembly ใดๆ, Derived Class ใดๆ
privateContaining Type OnlyClass/Struct เดียวกันเท่านั้น
protectedContaining + DerivedClass เดียวกัน + Derived Class (ทุก Assembly)
internalCurrent AssemblyClass ใดๆ ใน Assembly เดียว
protected internalUnion: Protected OR InternalSame Assembly OR Derived Class (Any Assembly)
private protectedIntersection: Protected AND InternalSame 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

ContextDefault
Class/Struct (Namespace Level)internal
Class/Struct (Nested)private
Interface Memberspublic (Implicit)
Enum Memberspublic
Delegateinternal (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 แทน)