c#-namespaces

Source: Internet
Author: User

namespaces provide a way to organize related classes and other types. Unlike files or components, namespaces are a logical combination, not a physical combination. When you define a class in a C # file, you can include it in the namespace definition. Later, when you define another class and then perform related operations in another file, you can include it in the same namespace, creating a logical combination that tells the other developers who use the class how the two classes are related and how they are used:

Namespace Customerphonebookapp
{
Using System;
public struct subscriber
{
Code for struct here ...
}
}

Placing a type in a namespace effectively assigns a longer name to the type, which includes the namespace of the type, followed by a period (.). and the name of the class. In the example above, the full name of the subscriber structure is customerphonebookapp.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 a namespace, creating hierarchies for types:

Namespace Wrox
{
Namespace Procsharp
{
Namespace Basics
{
Class Namespaceexample
{
Code for the class here ...
}
}
}
}

Each namespace name consists of the names of its namespaces, separated by periods, first the outermost namespace, and finally its own short name. So the full name of the Professionalcsharp namespace is the full name of the Wrox.procsharp,namespaceexample class is Wrox.ProCSharp.Basics.NamespaceExample.

You can also use this syntax to organize namespaces in your own namespace definitions, so the above code can also be written as:

Namespace Wrox.ProCSharp.Basics
{
Class Namespaceexample
{
Code for the class here ...
}
}

Note It is not allowed to declare a multi-part namespace in another nested namespace.

The namespace is independent of the Assembly. You can have different namespaces in the same assembly, or you can define types in the same namespace in different assemblies.

Using statement

Obviously, namespaces are fairly long and cumbersome to type, and it's not necessary to specify a particular class in this way. As described at the beginning of this chapter, C # allows the full name of a shorthand class. To do this, list the namespace of the class at the top of the file, preceded by the Using keyword. Elsewhere in the file, you can use its type name to refer to a type in the namespace:

Using System;
Using Wrox.procsharp;

As mentioned earlier, all C # source code begins with the statement using system, only because many of the useful classes Microsoft provides are included in the System namespace.

If the two namespace referenced by the using directive contains a class with the same name, you must use the full name (or at least a longer name) to ensure that the compiler knows which type to access, for example, Class namespaceexample exist in both the Wrox.ProCSharp.Basics and Wrox.ProCSharp.OOP namespaces, if you want to create a class test in the namespace Wrox.procsharp and instantiate a namespacee in the class Xample class, you need to specify which 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 is at the beginning of the C # file, C and C + + also place the # include here, so programmers migrating from C + + to C # often confuse namespaces with C + + style header files. Do not make this mistake, and the using statement does not actually establish a physical link between the files. C # also does not have a section that corresponds to a C + + header file.

Companies should spend a certain amount of time developing a namespace pattern so that their developers can quickly locate the features they need, and the class names used within the company do not conflict with the external class libraries. The rules and other naming conventions that establish the namespace pattern are described later in this chapter.

Aliases for namespaces

Another use of the Using keyword is to assign aliases to classes and namespaces. If the name of the namespace is very long and is used more than once in your code, but you do not want the namespace's name to be included in the using directive (for example, to avoid class name collisions), you can specify an alias for the namespace with the following syntax:

Using alias = NamespaceName;

The following example (a revised version of the previous example) assigns an alias introduction to the Wrox.ProCSharp.Basics namespace and instantiates an Namespaceexample object with this alias, which is defined in the namespace. It has a method, GetNamespace (), that invokes the GetType () method for each class to access the type object that represents the class. The following object is used 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;
}
}
}

——————————————————

Additional information to learn: https://docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/namespaces/

c#-namespaces

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.