SoFunction
Updated on 2025-03-07

Summary of Internal keywords in C#

First, clarify a few concepts: project, solution, assembly, namespace.

ProjectIt is a software we developed. Under .NET, there are many types of projects, such as consoles, Windows applications, class libraries, web applications, etc. After compilation, the .exe file and .dll file will be generated. The .exe file has a unified main program entry that can be executed, while the class library only provides some functions for other projects to call.

SolutionWhen we create any type of project in VS, this project is also a solution. When our business is relatively simple, the role of solutions is not very good. But when we develop complex software, we need to make up multiple modules. For example, the three-layer architecture commonly used in development, the U layer is a simple windows application (a type of project), and the B and D layers are composed of multiple class libraries (another type of project). With one solution, we can combine multiple projects to complete our development. Factually speaking, the solution is a container, which is divided into many layers and many grids to store different projects. In other words, assemblies are one project, and multiple projects form a solution.
Assembly A project is an assembly. An assembly can be embodied as a dll file, or an exe file.

NamespaceMainly to avoid conflicts of the same object names that may exist in a project.

Editorially speaking, the namespace simply adds some symbols separated by dots before the type name, which makes a type name longer and thus more unique. If two identical classes are in the same namespace, they will conflict, and if different namespaces have the same type, ambiguity will also occur.

Note that the using indicator of C# will instruct the compiler to try to add a different prefix to the type name until a match is found. Namespaces are just logically, and the real type is in the assembly. When looking for a definition of a type, the compiler must be told to which assembly to search, and the compiler will scan all assembly it knows to find the definition of the type. Once the compiler finds the correct program, assembly information and type information are added to the metadata that generates the hosted module.
Important: CLR doesn't know anything about namespaces. When accessing a type, the CLR needs to know the full name of the type (which may be a fairly long name containing a period) and which assembly the type is defined in. This allows the "runtime" to load the correct assembly, find the target type, and operate on it.

Contact and Difference:

Namespace is the logical organization form of a class library, and assembly is the physical organization form of a class library.

There may be multiple namespaces within an assembly, and one namespace may exist in different assembly settings.

Assembly is an implementation type file and is generated after compilation. A namespace is a logical grouping of types.

The C# compiler may be more concerned with namespaces because it needs to determine the full name of the class and then hand it over to the CLR. CLR only cares about the assembly and will load the corresponding assembly through the full name of the class.

Summarize:

By using participle classes in the project, it is found that the expansion and improvement of this class can be achieved through participle classes. When using a branch class, we will modify the namespace and then expand the class. This is what we call a namespace that can exist in different assembly. Through projects, you can continue to grow and work hard. For the introduction to the division category, you can visit the following address:/zh-cn/library/

1. Internal: It is limited to only accessible in the same assembly and can be cross-class

protected: It is limited to only accessible in inherited subclasses and can be cross-assembly
protected internal: The protected "or" internal modifier modifies a member. When the parent class and the child class are in the same assembly, the internal member is visible. When the parent class and the child class are not in the same assembly, the child class cannot access the parent class internal member, and the child class can access the ptotected internal member of the parent class.
That is, types or members with access modifier protected internal can be accessed from the current assembly or type derived from the containing class.

2. The internal keyword is a member access modifier of type and type. Internal types or members are accessible only in files of the same assembly. Internal access is often used for component-based development because it enables a group of components to collaborate in a private way without having to be exposed to the rest of the application code. For example, a framework for generating a graphical user interface can provide Control and Form classes, which collaborate by using members with internal access. Since these members are internal, they are not exposed to code that is using the framework.

3. It is wrong to refer to the type or member from an assembly that defines an internal access capability. Example 1 contains two files (indicates that the two files are not in the same assembly): and . The first file contains the internal base class BaseClass, and in the second file, an attempt to instantiate BaseClass will produce an error:

Example 1:

 // 
 internal class BaseClass 
 {
   public static int intM = 0;
 }
 // 
 // Compile with:
 class TestAccess 
 {
   static void Main() 
   {
     BaseClass myBase = new BaseClass();  // Error, unable to instantiate   }
 }

In Example 2, use the same file as used in Example 1 and change the accessibility level of BaseClass to public, and also change the accessibility level of member IntM to internal. In this example, the class can be instantiated, but its internal members cannot be accessed:

Example 2:

 // 
 public class BaseClass 
 {
   internal static int intM = 0;
 }
 // 
 // Compile with:
 public class TestAccess 
 {
   static void Main() 
   {
     BaseClass myBase = new BaseClass();  // Ok, you can instantiate the class      = 444;  // Error because internal members cannot be accessed  }
 }

The above is the summary of Internal keywords in C# introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!