Detailed explanation of internal keywords in C#

1. Internal
We all know the three types/member modifiers public, protected , and private, and we also understand the meanings they represent. Internal is also a type/member modifier (the modified type or member is called an internal type or member ), but the class it modifies can only be accessed in the same assembly, and the same assembly means the same dll assembly or the same exe assembly. A project in vs will generate a dll file, so this dll or this project is also an assembly.
For example, there is an assembly with:

namespace Common
{
    internal class A
    {
        public void aa()
        {
        }
    }
    public class B
    {
        public void bb()
        {
            //这样是正确的
            A a = new A();
            a.aa();
        }
    }
} 

If another project TestWeb references this dll, common.dll, ie

using Common;
namespace TestWeb
{
    public class Test
    {
        public void test_1()
        {
            //这样是出错的
            A a = new A();
            a.aa();
 
            //这样是正确的
            B b = new B();
            b.bb();
        }
    }
}

Internal access is often used in component-based development because it enables a set of components to cooperate in a private manner without having to be exposed to the rest of the application code. For example, a framework for generating graphical user interfaces might provide Control and Form classes that cooperate by using members with internal access rights. Since these members are internal, they are not exposed to code that is using the framework.
2. Protected internal
Generally, a member or type can only have one access modifier, except when using protected internal combination, its access is limited to the current assembly or type derived from the containing class. That is, we can access it in this assembly, and can also inherit access in other assemblies.
as follows:
 

namespace Common
{
    protected internal class C
    {
        public void cc()
        {
        }
    }
} 
 
namespace TestWeb
{
    public class TestA
    {
        public void test_1()
        {
            //这样是出错的
            Common.C c = new Common.C();
            c.cc();
        }
    }
    public class TestB : Common.C
    {
        public void test_2()
        {
            //这样是正确的
            C c = new C();
            c.cc();
        }
    }
}

In addition, the characteristics of the assembly are attached:

1. An assembly is the basic building block of any .NET Framework application. For example, when building a simple C# application, Visual Studio creates an assembly in the form of a single portable executable (PE) file, specifically an EXE or DLL.
2. Contains metadata describing their own build number and details of all data and object types they contain.
3. Only load when needed. If the assembly is not used, it will not be loaded. This means that assemblies can be an effective way to manage resources in large projects.
4. Can contain one or more modules. For example, when planning a larger project, you can have several individual developers work on individual modules and create a single assembly by combining all of these modules.

Guess you like

Origin blog.csdn.net/weixin_42565127/article/details/130927438