C # access modifier internal access scope misunderstanding explanation

Source: Internet
Author: User
Tags modifier modifiers mscorlib
First, the preface

MSDN Access-level explanations for access modifiers:

An access modifier is a keyword that specifies the accessibility of a declared member or type. This section describes the four access modifiers:

Public

Protected

Internal

Private

Use these access modifiers to specify the following five levels of accessibility:

Public: Access is unrestricted.

Protected: Access is limited to types that contain classes or derive from the containing class.

Internal: Access is limited to the current assembly.

Protected internal: Access is restricted to the current assembly or a category that contains derived types.

Private: Access is limited to the containing type. second, cognitive misunderstanding

Internal, the meaning of English is "internal", at this time the foundation is not solid classmate, may confuse this "internal" the exact meaning, in the end refers to "the same namespace" inside, or "the same assembly" internal, I was suddenly asked this question, really hesitate, And once thought to be "the same namespace" inside (words: We too often think, in fact, as long as their own a little MSDN check, it is very clear).

The first part has already extracted the official explanation of Microsoft MSDN, in fact, this interior is "the same assembly" internal, that is,internal modified method or attribute, as long as in the same assembly of other classes can access, if the two are not in the same namespace, As long as you use the appropriate namespace on the using reference, here, in another respect, the namespace is not defined as the access level, but to ensure that the global class uniqueness, the following is the real life to explain the actual role of internal. Third, the explanation

All say art originates from life, programming is also an art, so it is also possible to find the corresponding scene in life, below we use the actual life scene to describe the role of internal modifier.

A technical center of a company, issued by the day, the center of the "printer" is limited to the center of the use of various departments, other centers can not use the personnel. This real life scene, the technology center is actually an assembly, and each department is equivalent to different classes, of course, the department can have the corresponding label, the equivalent of different namespaces, in fact, is the logical division, the responsibility of different departments belong to different namespaces can be understood. And our "protagonist" printer will be posted next to the bulletin "The center of the printer can only be used in the center ...." , this is the equivalent of a internal modifier to the printer. Other students in the center, no longer use the center of the printer.

Here we use code to describe the above scenario:

We first define a printer's class in a common class library (assembly)

Namespace Commonasset
{public
    class Printer
    {
        private bool _isbad;
        <summary>
        ///printer is bad
        ///</summary> public
        bool Isbad
        {got
            {return _isbad;}
            set {_isbad = value;}
        }

        public void print ()
        {
            Console.WriteLine ("Start printing!");
        }
    }

And then we're creating a center a (assembly) that defines each department separately and belongs to a different namespace

Namespace Centera.administration
{public
    class DEPARTMENTB
    {
        internal Printer printerb {get; set;} Public
        DEPARTMENTB ()
        {
            Printerb = new Printer ();


}}} Namespace Centera.technology
{public
    class Departmenta
    {
        internal Printer printera {get; set;} Public
        Departmenta ()
        {
            Printera = new Printer ();
        }

        public void printsomething ()
        {
            if (Printera.isbad)
            {
                CenterA.Administration.DepartmentB DEPARTMENTB = new CenterA.Administration.DepartmentB ();
                DepartmentB.PrinterB.Print ();
            }
            else
            {
                printera.print ();
            }
    }}

As you can see, DEPARTMENTB and Departmenta are not in the same namespace, but because of the same Centera this assembly, Departmenta can access printer in DEPARTMENTB. the confused internal

Relationship between assembly and namespace: there may be multiple namespaces within an assembly, and a namespace may exist in different assemblies. As a matter of fact, there is no real connection between the two, they are just a type of division on both levels (logical and physical), for the compiler, the namespace simply adds a number of symbols separated by the type name, thereby specifying a type more precisely to prevent conflicts in the system; For a definition of a type that occurs in an assembly when the definition of a type is found, C # 's using directive specifier instructs the compiler to try to add a different prefix to the type name, and the compiler must be told which assembly to look for, and the compiler will scan all the assemblies 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 managed module.

to sum up, the scope of the internal is the same assembly and not the same namespace. Clear assembly and namespace relationships

Namespaces allow us to logically organize related types, which makes it easy to locate a type:
To give a simple example, we instantiate a queue,
If you do not introduce namespaces, you write this:

System.Collections.Queue q=new System.Collections.Queue ();

What about the introduction of namespaces? Just write this:

Using system.collections;//introduces namespace
queue q=new queue ();

For editing, the namespace simply adds a number of symbols separated by the point number before the type name. This makes the name of a type longer and thus more unique. If two identical classes are conflicting in the same namespace, if the different namespaces have the same type, there are two meanings, Just like a directory in Windows, the same directory cannot have files of the same name, and different directories can. If you want to use, you can use the full name (System.Collections.Queue), or using sysqueue= System.Collections.Queue;

Note that C # 's using directive specifier instructs the compiler to try to add a different prefix to the type name. Until a match is found, as has been said, the namespace is only logically, the true type is in the assembly, and when you look up a definition of a type, the compiler must be told which assemblies to look for, The compiler scans all the assemblies 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 managed module, C # The compiler automatically finds it in the MSCorLib.dll assembly by default. The MSCorLib.dll contains the core types defined in all FCL. such as object,string.

Relationship between namespaces and assemblies: each type that is subordinate to a namespace may be implemented in a different assembly. For example: System. Io. The FileStream type is implemented in the MSCorlib.dll assembly, and the System.IO.FileSystemWatcher type is implemented in the System.dll assembly. In fact, the. Net framework does not even publish a System.IO.dll assembly at all. (There may be more than one namespace in an assembly, and the same namespace may exist in different assemblies.) In an assembly, it may also contain types in different namespaces. For example, both the System.Int32 and System.Text.StringBuilder types are in the MSCorLib.dll assembly. When you look up a type in the. NET Framework SDK documentation, the document explicitly indicates the namespace to which the type belongs, and the assembly that implemented the type. (using an image metaphor: a type that has two IDs, one is a namespace, the other is an assembly)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.