Summary of Internal keywords in C

  • 2021-12-12 09:30:40
  • OfStack

First, clarify several concepts: project (project), solution (solution), assembly (assembly), namespace (namespace).

Project (project) is a software developed by us. Under NET, there are many types of projects, such as consoles, Windows applications, class libraries, Web applications, and so on. When compiled, the. exe file and. dll file are generated. The. exe file has the main program entry of the system 1, which can be executed, while the class library only provides 1 function for other projects to call.

Solution (solution) When we create any one type of project in VS, the project is still a solution. When our business is relatively simple, the solution does not play a great role. But when we develop complex software, we need multiple modules. For example, in the three-tier architecture commonly used in development, U layer is a simple windows application (one type of project), and B and D layers are composed of multiple class libraries (another type of project). With one solution, we can combine its multiple projects to complete our development. Figuratively speaking, the solution is a container, which is divided into many layers and compartments for storing different projects. In other words, an assembly is a project, and multiple projects constitute a solution.
Assemblies (assembly) A project is an assembly. An assembly can be embodied as an dll file or an exe file.

Namespace (namespace) is primarily designed to avoid conflicts of the same object name that might exist in a project.

Edit-wise, the namespace is simply a dot-separated symbol preceding the type name, which makes the name of a type longer and therefore more unique. If two identical classes are in the same namespace, it will conflict, and if different namespaces have the same type, it will also produce duality.

Note that the using indicator of C # instructs the compiler to try to add a different prefix to the type name until a match is found. Namespace is only logical, and the real type is in the assembly. When looking for a definition of a type, the compiler must be told which assemblies to look for, and the compiler scans all assemblies it knows to look for the definition of the type. 1 Once the compiler finds the correct program, assembly information and type information are added to the metadata that generated the managed module.
Important note: CLR doesn't know anything about namespaces. When accessing a type, CLR needs to know the full name of the type (which may be a fairly long name with a period symbol) and the specific assembly in which the type is defined. This way, the "runtime" can load the correct assembly, find the target type, and operate on it.

Connection and difference:

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

There may be multiple namespaces within 1 assembly, and 1 namespace may exist in different assemblies

Assemblies are files that implement types and are generated after compilation. 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 give it to CLR. CLR only cares about assemblies and loads the corresponding assembly by the full name of the class

Summary:

By using partial classes in the project, we find that this class can be extended and perfected by partial classes. When using partial classes, we will modify the namespace and then extend the class. This is what we call a namespace that can exist in different assemblies. Grow and work hard through the project. "The introduction of partial classes can be found at the following address: https://msdn.microsoft.com/zh-cn/library/wa80x488.aspx."

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

protected (Protected): Qualified to be accessible only in inherited subclasses and can be used across assemblies
protected internal: A protected "or" inner modifier modifies a member that is visible when the parent and child classes are in the same assembly. When the parent class and the child class are not in the same assembly, the child class cannot access the internal member of the parent class, while the child class can access the ptotected internal member of the parent class.
That is, a type or member with the access modifier protected internal can be accessed from the current assembly or from a type derived from the containing class.

2. The internal keyword is a type and a member access modifier for a type. Internal types or members are accessible only in files with the same 1 assembly. Internal access is commonly used for component-based development because it enables a set of 1 components to collaborate privately 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 that cooperate by using members with internal access. Because these members are internal, they are not exposed to the code that is using the framework.

3. It is an error to reference a type or member with internal access from outside the assembly that defines the type or member. Example 1 contains two files (meaning they are not in the same assembly): Assembly1. cs and Assembly2. cs. The first file contains the internal base class BaseClass, and in the second file, an attempt to instantiate BaseClass generates an error:

Example 1:


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

In Example 2, use the same file as 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 case, the class can be instantiated, but its internal members cannot be accessed:

Example 2:


 // Assembly1.cs
 public class BaseClass 
 {
   internal static int intM = 0;
 }
 // Assembly2.cs
 // Compile with : Assembly1.dll
 public class TestAccess 
 {
   static void Main() 
   {
     BaseClass myBase = new BaseClass();  // Ok You can instantiate a class 
     BaseClass.intM = 444;  //  Error because internal members cannot be accessed 
  }
 }

Related articles: