C# Protected and internal KeywordsUse the protected and internal keywords. Review the effects of these modifiers.
Protected and internal.
These are accessibility keywords. Protected controls how other types (like derived types) in a C# program can access a class and its members.Keywords
The protected modifier
is between the private and public domains. It is the same as private but allows derived classes to access the member.Public, private
Consider 2 classes, A and B. Class B is derived from A. If we look inside class A, we see it has 2 int fields: 1 protected and 1 private.ClassInt, uint
And: In class B, we can access the protected int, but not the private int. So protected gives us additional access in a derived class.
Note: The accessibility domain of protected fields includes all derived classes. This is a key difference.
C# program that uses protected modifier
protected int _a;
private int _b;
class B : A
// Can access protected int but not private int!
static void Main()
B b = new B();
The internal modifier, like others such as public and private, changes restrictions on where else the type can be accessed.
Example: We see an internal "Test" class (just for this example). Any member type can also be modified with internal.
Tip: You can have internal fields, properties, or methods. This program can instantiate the Test type because it is in the same program.
C# program that uses internal modifier
static void Main()
// Can access the internal type in this program.
Test test = new Test();
test._a = 1;
// Example of internal type.
internal class Test
public int _a;
Consider the C# specification. It states that "The intuitive meaning of internal is: access limited to this program."
So: In other words, no external program will be able to access the internal type.
Note: Accessibility in the C# language determines what regions of program text are allowed to access a specific member.
The internal keyword is mainly for information hiding. This improves program quality by forcing programs to be modular.
Thus: A C# program that is contained in a different binary file (such as DLL or EXE) will not successfully access the internal member.
Maintenance: In large programs, maintenance is a huge issue. Information hiding (helped with internal) helps here.
Tip: In most programs, internal is not needed. For larger, more complex programs, it becomes more useful.
The "protected internal" modifier is a special case in the C# language. It is not precisely the same as both protected and internal.
Details: Protected internal means both internal and protected. The "internal" means only the current program can use the member.
However: With protected internal, derived classes in other programs can use the member. The modifier does not prevent this.
Thus: Protected internal is less restrictive than just protected. And it is not as common in programs.
A summary. The protected modifier, along with internal, restrict access to members in specific ways. Internal restricts a member to the current program.
And protected, meanwhile, is used with class derivation. Protected makes it easier to keep a good level of information hiding while still allowing a rich object hierarchy.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to email@example.com.