Visual CSharp namespace details

Source: Internet
Author: User

The namespace provides a way to organize related classes and other types. Unlike a file or component, a namespace is a logical combination, not a physical combination. When defining a class in the C # file, you can include it in the namespace definition. Later, when defining another class and executing relevant operations in another file, you can include it in the same namespace and create a logical combination, tell other developers who use classes how these two classes are related and how to use them:

namespace CustomerPhoneBookApp{  using System;  public struct Subscriber  {  // Code for struct here...  }}

When you place a type in a namespace, You can effectively specify a long name for this type. This name includes the type namespace followed by the period (.) and Class names. In the preceding example, the full name of the Subscriber structure is mermerphonebookapp. Subscriber. In this way, different classes with the same short name can be used in the same program.
You can also nest other namespaces in The namespace to create a level for the type:

namespace Wrox{    namespace ProCSharp    {        namespace Basics        {            class NamespaceExample            {                // Code for the class here...            }        }    }}

Each namespace name consists of the name of the namespace in which it is located. These names are separated by periods (,). They are the namespace at the outermost layer, and their own short names. Therefore, the full name of the ProfessionalCSharp namespace is Wrox. ProCSharp, and the full name of the NamespaceExample class is Wrox. ProCSharp. Basics. NamespaceExample.
With this syntax, you can also organize the namespaces defined in your namespace, so the above Code can also be written:

namespace Wrox.ProCSharp.Basics{    class NamespaceExample    {        // Code for the class here...    }}

Note that multiple namespaces cannot be declared in another nested namespace.
The namespace has nothing to do with the Assembly. You can have different namespaces in the same dataset or define the types in the same namespace in different datasets.
Using statement
Obviously, the namespace is quite long and tedious to type. It is not necessary to specify a specific class in this way. As described at the beginning of this chapter, C # allows the full name of a short class. To this end, you need to list the class namespace at the top of the file, and add the using Keyword before it. Elsewhere in the file, you can use its type name to reference the type in the namespace:

Using System;
Using Wrox. ProCSharp;

As mentioned above, all C # source code starts with the statement using System; because many useful classes provided by Microsoft are included in the System namespace.
If the using command references two namespaces that contain classes of the same name, you must use a complete name (or at least a long name) to ensure that the compiler knows which type to access, for example, the NamespaceExample class also exists in Wrox. proCSharp. basics and Wrox. proCSharp. in the OOP namespace. create a class Test in ProCSharp and instantiate a NamespaceExample class in this class. You need to specify the class to use:

using Wrox.ProCSharp;class Test{    public static int Main()    {        Basics.NamespaceExample nSEx = new Basics.NamespaceExample();        //do something with the nSEx variable        return 0;    }}

Because the using statement starts with the C # file, C and C ++ put # include here, therefore, programmers who migrate data from C ++ to C # often confuse namespaces with C ++-style header files. Do not make this mistake. The using statement does not really establish physical links between these files. C # does not correspond to the C ++ header file.
The company should spend some time developing a namespace model so that its developers can quickly locate the features they need, in addition, the internal class names used by the company do not conflict with external class libraries. This chapter describes the rules for creating namespaces and other naming conventions.
Namespace alias
Another purpose of the using keyword is to specify an alias for the class and namespace. If the namespace name is very long and needs to be used multiple times in the code, but you do not want the namespace name to be included in the using command (for example, to avoid class name conflicts ), you can specify an alias for the namespace. The syntax is as follows:
Using alias = NamespaceName;
The following example (the revised version of the previous example) is provided to Wrox. proCSharp. the Basics namespace specifies the alias Introduction and uses this alias to instantiate a NamespaceExample object defined in this namespace. It has a method GetNamespace (), which calls the GetType () method of each class to access the Type object that represents the class Type. The following uses this object to return the namespace name of the class:

using System;using Introduction = Wrox.ProCSharp.Basics;class Test{    public static int Main()    {        Introduction.NamespaceExample NSEx = new Introduction.NamespaceExample();        Console.WriteLine(NSEx.GetNamespace());        return 0;    }}namespace Wrox.ProCSharp.Basics{    class NamespaceExample    {        public string GetNamespace()        {            return this.GetType().Namespace;        }    }}

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.