Export (0) Print
Expand All
10 out of 20 rated this helpful - Rate this topic

namespace

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:

name, name1
A namespace name can be any legal identifier. A namespace name can contain periods.
type-declarations
Within a namespace, you can declare one or more of the following types:

Remarks

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.

3.8 Namespace and type names and 9. Namespaces discuss namespaces in more detail.

See 7.3 Member lookup for information about how the compiler resolves names.

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 
   {
   }
}

Example

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");
         }
      }
   }
}

Output

Hello

See Also

C# Keywords | using

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.