visual| Namespaces | Detailed namespaces provide 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 you define a class in a C # file, you can include it in the namespace definition. Later, when you define another class and perform related operations in another file, you can include it in the same namespace, create a logical combination, tell the other developers who use the class how these two classes are related, and how to use them:
Namespace Customerphonebookapp
{
Using System;
public struct subscriber
{
Code for struct ...
}
}
Putting a type in a namespace can effectively specify a long name for the type that includes the namespace of the type, followed by a period (.) and the name of the class. In the above example, 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 namespaces and create hierarchies for types:
Namespace Wrox
{
Namespace Procsharp
{
Namespace Basics
{
Class Namespaceexample
{
Code for the class here ...
}
}
}
}
Each namespace name consists of the names of the namespaces in which they are separated by a period, first the outermost namespace, and finally its own short name. So the full name of the Professionalcsharp namespace is the Wrox.procsharp,namespaceexample class's full name is Wrox.ProCSharp.Basics.NamespaceExample.
This syntax can also be used to organize namespaces in your own namespace definition, 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 multiple-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, the namespace is quite long, it's cumbersome to type, and it's not necessary to specify a particular class in this way. As explained at the beginning of this chapter, C # allows the full name of the abbreviated 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 the type in the namespace:
Using System;
Using Wrox.procsharp;
As mentioned earlier, all C # source code starts with the statement using System, only because many of the useful classes that Microsoft provides are contained in the System namespace.
If the two namespaces referenced by the using directive contain classes 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 at the beginning of the C # file, C and C + + also put #include here, programmers migrating from C + + to C # often confuse namespaces with C + + style header files. Do not make this mistake, the using statement does not actually establish a physical link between these files. C # also does not have a section that corresponds to the C + + header file.
Companies should spend a certain amount of time developing a namespace pattern so that their developers can quickly locate the functionality they need, and the class names used within the company will not conflict with the external class library. The rules and other naming conventions that establish the namespace pattern are described later in this chapter.
Aliases for namespaces
Another purpose of the Using keyword is to specify an alias for the class and namespace. If the name of the namespace is very long and is used more than once in your code, but you do not want the name of the namespace to be contained in the using directive (for example, to avoid a class name conflict), you can assign an alias to the namespace with the following syntax:
Using alias = NamespaceName;
The following example (a revision of the previous example) assigns an alias introduction to the Wrox.ProCSharp.Basics namespace and uses this alias to instantiate a Namespaceexample object that is defined in the namespace. It has a method GetNamespace (), which calls the GetType () method of each class to access the type object that represents the class. This object is used below 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;
}
}
}
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