Sobes.tech
Junior — Middle

What is the difference between the access modifiers protected and internal in programming languages?

sobes.tech AI

Answer from AI

In C#, access modifiers protected and internal have different scopes:

  • protected — access is allowed within the class and all its derived classes, even if they are in different assemblies.
  • internal — access is allowed only within the current assembly, meaning all classes in the same project but not from other assemblies.

There is also a combined modifier protected internal, which allows access either from derived classes or from the current assembly.

Example:

class BaseClass {
    protected void ProtectedMethod() { }
    internal void InternalMethod() { }
}

class DerivedClass : BaseClass {
    void Test() {
        ProtectedMethod(); // accessible
        InternalMethod();  // accessible if in the same assembly
    }
}

class OtherClass {
    void Test() {
        var b = new BaseClass();
        // b.ProtectedMethod(); // error, not a subclass
        // b.InternalMethod();  // accessible if in the same assembly
    }
}