TheDeveloperBlog.com


C# Internal Keyword

Internal restricts access to members. There are many accessibility modifiers you can apply to a type. The internal modifier, like others such as public and private, changes restrictions on where else the type can be accessed.


Example. To begin, we look at an example of the internal keyword in a C# program. You can add the internal modifier right before the keyword "class" to create an internal class. 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.

Based on:

.NET 4.5

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;
}


Specification. What does the internal modifier do exactly? The C# specification is concise on this question. 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.


Use. As we have seen, the internal modifier has a specific purpose. What are some examples of its usage? Mainly, internal is for information hiding. This improves program quality by forcing programs to be modular.

This means that if you have a C# program that is contained in a different binary file, such as DLL or EXE, it will not successfully access the internal member. And this leads to improved maintainability.

Tip: In most programs, internal is not needed. For larger, more complex programs, it becomes more useful.

Also: The material on the public and private modifiers helps us understand internal and its effects.

PublicPrivate

Summary. The internal keyword enables information hiding across program boundaries. It is an accessibility modifier in the C# language. It is not needed in most programs. But it can improve the ease of maintenance on much larger programs.