Set programs and namespaces in C #

Source: Internet
Author: User

The namespace allows us to logically organize related types, which makes it easy to locate a type:
For a simple example, we instantiate a queue,
If the namespace is not introduced, write as follows:
System. Collections. Queue q = new system. Collections. Queue ();
What about introducing a namespace? Write as follows:
Using system. collections; // introduce the namespace
Queue q = new Queue ();
For editing, the namespace only adds some symbols separated by periods before the type name, which makes the name of a type longer,This makes it more unique.. If two identical classes are in the same namespace, they will conflict. If different namespaces have the same type, they will also produce ambiguity, just like directories in windows, files with the same name cannot exist in the same directory, but different directories can. if you need to use it, you can use the full name (System. Collections. Queue), OrUsing sysqueue = system. Collections. Queue;

Note that the C # using indicator instructs the compiler to try to add different prefixes to the type name until a match is found,The namespace is logical, and the real type is in the set.When you look up a type definition, the compiler must be informed of which sets to look up. The compiler will scan all the sets it knows to look up the type definition. once the compiler finds the correct program, the assembly information and type information will be added to the metadata of the generated managed module. By default, the C # compiler will automatically go to mscorlib. DLL programs are centrally searched. mscorlib. dll contains all the core types defined in FCL. such as object and string.

Relationship between namespaces and assembly:
1.All types of namespaces may be implemented in different programming sets. For example, the system. Io. filestream type is implemented in the mscorlib. dll program, while the system. Io. filesystemwatcher type is implemented in the system. dll program. In fact,. NET Framework does not even publish a system. Io. dll assembly. (An assembly may have multiple namespaces, and the same namespace may exist in different programming sets .)
2.In a program set, it may also contain types in different namespaces. For example, the system. int32 and system. Text. stringbuilder types are all in the mscorlib. dll program set.
3.When you search for a type in the. NET Framework SDK documentation, the documentation explicitly specifies the namespace to which the type belongs and the Assembly that implements this type.
(An image is used as a metaphor: it has two ID cards, one is the namespace and the other is the Assembly)

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.