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.
Section 3.6 and chapter 9 of the language reference discuss namespaces in more detail.
See 7.3 Member lookup for information about how the compiler resolves names.
See Access Modifiers for a discussion on the access modifiers that 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 { } }
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
C# Keywords | Grammar