TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# Protected and internal Keywords

Use 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
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.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 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(); } } Output 0
Internal example. 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 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.

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.

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.

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.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf