Encapsulation of Types

Just as classes serve as an encapsulation boundary for behavior and data, so assemblies provide for similar boundaries among groups of types. Developers can break a system into assemblies and then share those assemblies with multiple applications or integrate them with assemblies provided by third parties.

public or internal Access Modifiers on Type Declarations

By default, a class or struct without any access modifier is defined as internal.1 The result is that the class is inaccessible from outside the assembly. Even if another assembly references the assembly containing the class, all internal classes within the referenced assemblies will be inaccessible.

Just as private and protected provide levels of encapsulation to members within a class, so C# supports the use of access modifiers at the class level for control over the encapsulation of the classes within an assembly. The access modifiers available are public and internal. To expose a class outside the assembly, the assembly must be marked as public. Therefore, before compiling the Coordinates.dll assembly, it is necessary to ensure the type declaration is public (see Listing 10.8).

Listing 10.8: Making Types Available outside an Assembly
public struct Coordinate
{
    // ...
}
 
public struct Latitude
{
    // ...
}
 
public struct Longitude
{
    // ...
}
 
public struct Arc
{
    // ...
}

Similarly, declarations such as class and enum can be either public or internal.2 The internal access modifier is not limited to type declarations; that is, it is also available on type members. Consequently, you can designate a type as public but mark specific methods within the type as internal so that the members are available only from within the assembly. It is not possible for the members to have a greater accessibility than the type. If the class is declared as internal, public members on the type will be accessible only from within the assembly.

The protected internal Type Modifier

Another type member access modifier is protected internal. Members with an accessibility modifier of protected internal will be accessible from all locations within the containing assembly and from classes that derive from the type, even if the derived class is not in the same assembly. The default member access modifier is private, so when you add an access modifier (other than public), the member becomes slightly more visible.

note
Members with an accessibility modifier of protected internal will be accessible from all locations within the containing assembly and from classes that derive from the type, even if the derived class is not in the same assembly.
The file Type Modifier

C# 11 introduced a new access modifier, the file modifier. Decorating a type with the file modifier restricts its visibility to within the file only. A file class Thing{ } defined in one file would not be visible outside the file. In fact, you could have multiple files with the same project all defined as file class Thing{} and there would be no conflict since they would not be visible to each other. The file modifier is mostly used in code generation so that and types generated specifically for internal purposes would not affect existing code, and, in fact, the same code generation in multiple files would be encapsulated locally to the file.3

Beginner Topic
Type Member Accessibility Modifiers

The full list of access modifiers appears in Table 10.1.

Table 10.1: Accessibility Modifiers

Modifier

Description

public

The member is accessible anywhere the type is accessible.

internal

The member is accessible from within the assembly only.

private

The member is accessible from within the containing type but inaccessible otherwise.

protected

The member is accessible within the containing type and any subtypes derived from it, regardless of the assembly.

protected internal

The member is accessible from anywhere within the containing assembly and from any types derived from the containing type, even if the derived types are within a different assembly.

private protected

The member is accessible from any types derived from the containing type that are also in the same assembly.4

file

The member is accessible only from the same file.

________________________________________

1. Excluding nested types, which are private by default.
2. . You can decorate nested classes with any access modifier available to other class members (e.g., private). However, outside the class scope, the only access modifiers that are available are public and internal.
3. file cannot be used for nested type definitions—only top-level definitions.
4. Added in C# 7.2.
{{ snackbarMessage }}
;