C# Internal

The C# programming language

Internal restricts access to members. In the C# language, there is an assortment of accessibility modifiers you can apply to a type. The internal modifier, like other such as public and private, changes the restrictions on where else the type can be accessed.

Keywords

This C# example program shows the effect of the internal modifier on a class.

Example

Note

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—you can have internal fields, properties, or methods. This program can instantiate the Test internal type because it is located in the same program.

Program that uses internal modifier [C#]

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;
}
Question and answer

Specification

So what does the internal modifier do exactly? The C# specification is very concise on this question and it states that "The intuitive meaning of internal is 'access limited to this program.'" In other words, no external program will be able to access the internal type. 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 very specific purpose; what are some examples of its usage? Mainly, internal is for information hiding: 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. Information hiding leads to improved maintainability, and also possible security pluses.

Tip: In most simple programs, internal is not needed.

Summary

Four-way arrow

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

Class Examples
.NET