The namespace keyword is used to declare a scope. This namespace scope lets you organize code and gives you a way to create globally-unique types.
namespace name[.name1] ...] {
type-declarations
}
where:
Even if you do not explicitly declare one, a default namespace is created. This unnamed namespace, sometimes called the global namespace, is present in every file. Any identifier in the global namespace is available for use in a named namespace.
Namespaces implicitly have public access and this is not modifiable.
See
See Access Modifiers for a discussion of the access modifiers you can assign to elements within a namespace.
It is possible to define a namespace in two or more declarations. For example, the following sample defines both classes as part of namespace MyCompany:
namespace MyCompany.Proj1
{
class MyClass
{
}
}
namespace MyCompany.Proj1
{
class MyClass1
{
}
}
The following example shows how to call a static method in a nested namespace.
// cs_namespace_keyword.cs
using System;
namespace SomeNameSpace
{
public class MyClass
{
public static void Main()
{
Nested.NestedNameSpaceClass.SayHello();
}
}
namespace Nested // a nested namespace
{
public class NestedNameSpaceClass
{
public static void SayHello()
{
Console.WriteLine("Hello");
}
}
}
}
Hello