protected, internal Keywords
This page was last reviewed on Dec 8, 2022.
Dot Net Perls
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.
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
Protected example. 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.
int, 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.
using System; class A { protected int _a; private int _b; } class B : A { public B() { // Can access protected int but not private int! Console.WriteLine(this._a); } } class Program { 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.
class 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.
No updates found for this page.
© 2007-2024 Sam Allen.