| title | Using Namespaces (C# Programming Guide) | ||
|---|---|---|---|
| ms.date | 07/20/2015 | ||
| ms.prod | .net | ||
| ms.technology |
|
||
| ms.topic | article | ||
| f1_keywords |
|
||
| helpviewer_keywords |
|
||
| ms.assetid | 1fe8bf39-addc-438a-bd9e-86410e32381d | ||
| caps.latest.revision | 26 | ||
| author | BillWagner | ||
| ms.author | wiwagn |
Namespaces are heavily used within C# programs in two ways. Firstly, the .NET Framework classes use namespaces to organize its many classes. Secondly, declaring your own namespaces can help control the scope of class and method names in larger programming projects.
Most C# applications begin with a section of using directives. This section lists the namespaces that the application will be using frequently, and saves the programmer from specifying a fully qualified name every time that a method that is contained within is used.
For example, by including the line:
[!code-csharpcsProgGuide#1]
At the start of a program, the programmer can use the code:
[!code-csharpcsProgGuide#31]
Instead of:
[!code-csharpcsProgGuide#30]
The using Directive can also be used to create an alias for a namespace. For example, if you are using a previously written namespace that contains nested namespaces, you might want to declare an alias to provide a shorthand way of referencing one in particular, as in the following example:
[!code-csharpcsProgGuideNamespaces#7]
The namespace keyword is used to declare a scope. The ability to create scopes within your project helps organize code and lets you create globally-unique types. In the following example, a class titled SampleClass is defined in two namespaces, one nested inside the other. The . Operator is used to differentiate which method gets called.
[!code-csharpcsProgGuideNamespaces#8]
Namespaces and types have unique titles described by fully qualified names that indicate a logical hierarchy. For example, the statement A.B implies that A is the name of the namespace or type, and B is nested inside it.
In the following example, there are nested classes and namespaces. The fully qualified name is indicated as a comment following each entity.
[!code-csharpcsProgGuideNamespaces#9]
In the previous code segment:
-
The namespace
N1is a member of the global namespace. Its fully qualified name isN1. -
The namespace
N2is a member ofN1. Its fully qualified name isN1.N2. -
The class
C1is a member ofN1. Its fully qualified name isN1.C1. -
The class name
C2is used two times in this code. However, the fully qualified names are unique. The first instance ofC2is declared insideC1; therefore, its fully qualified name is:N1.C1.C2. The second instance ofC2is declared inside a namespaceN2; therefore, its fully qualified name isN1.N2.C2.
Using the previous code segment, you can add a new class member, C3, to the namespace N1.N2 as follows:
[!code-csharpcsProgGuideNamespaces#10]
In general, use :: to reference a namespace alias or global:: to reference the global namespace and . to qualify types or members.
It is an error to use :: with an alias that references a type instead of a namespace. For example:
[!code-csharpcsProgGuideNamespaces#11]
[!code-csharpcsProgGuideNamespaces#12]
Remember that the word global is not a predefined alias; therefore, global.X does not have any special meaning. It acquires a special meaning only when it is used with ::.
Compiler warning CS0440 is generated if you define an alias named global because global:: always references the global namespace and not an alias. For example, the following line generates the warning:
[!code-csharpcsProgGuideNamespaces#13]
Using :: with aliases is a good idea and protects against the unexpected introduction of additional types. For example, consider this example:
[!code-csharpcsProgGuideNamespaces#14]
[!code-csharpcsProgGuideNamespaces#15]
This works, but if a type named Alias were to subsequently be introduced, Alias. would bind to that type instead. Using Alias::Exception insures that Alias is treated as a namespace alias and not mistaken for a type.
See the topic How to: Use the Global Namespace Alias for more information regarding the global alias.
C# Programming Guide
Namespaces
Namespace Keywords
. Operator
:: Operator
extern