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.
protected int _a;
private int _b;
class B : A
// Can access protected int but not private int!
static void Main()
B b = new B();
Internal example. The internal modifier, like others such as public and private, changes restrictions on where else the type can be accessed.
Detail 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.
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;
Internal, specification. 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.
Notes, internal. 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.
Detail 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.
Protected internal. The "protected internal" modifier is a special case in the C# language. It is not precisely the same as both protected and internal.
Detail 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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.