Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2010
Visual Studio
Visual C#
C# Reference
C# Keywords
 namespace
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2010/.NET Framework 4

Other versions are also available for the following:
Visual Studio 2010 - Visual C#
namespace (C# Reference)

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.

C#

namespace SampleNamespace
{
    class SampleClass { }

    interface SampleInterface { }

    struct SampleStruct { }

    enum SampleEnum { a, b }

    delegate void SampleDelegate(int i);

    namespace SampleNamespace.Nested
    {
        class SampleClass2 { }
    }
}

Within a namespace, you can declare one or more of the following types:

Whether or not you explicitly declare a namespace in a C# source file, the compiler adds a default namespace. This unnamed namespace, sometimes referred to as 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. For a discussion of the access modifiers you can assign to elements in a namespace, see Access Modifiers (C# Reference).

It is possible to define a namespace in two or more declarations. For example, the following example defines two classes as part of the MyCompany namespace:

C#
namespace MyCompany.Proj1
{
    class MyClass
    {
    }
}

namespace MyCompany.Proj1
{
    class MyClass1
    {
    }
}

The following example shows how to call a static method in a nested namespace.

C#
namespace SomeNameSpace
{
    public class MyClass 
    {
        static void Main() 
        {
            Nested.NestedNameSpaceClass.SayHello();
        }
    }

    // a nested namespace
    namespace Nested   
    {
        public class NestedNameSpaceClass 
        {
            public static void SayHello() 
            {
                Console.WriteLine("Hello");
            }
        }
    }
}
// Output: Hello

For more information, see the C# Language Specification. The language specification is the definitive source for C# syntax and usage.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Bad advice: one namespace per assembly      LukePuplett ... SJ at MSFT   |   Edit   |   Show History

Its often espoused that you should have one namespace per assembly. While an assembly should have a purpose and not be a dumping ground, I've found this leads to awkward API design.

The reason is that assemblies have two functions; a) a unit of distribution of types/code b) the ability to have internal and external implementation.

The latter is key, if you can only put all code related to a certain function in one namespace, then you've no way to layer-up the API so that you can release a built assembly into another team (a) that exposes the public API in a way that is self-documenting (the other team would presumably build upon the namespace and add their types to the same namespace, but within a different assembly (b)).

Edit by SJ at MSFT: I don't see that advice in the topic. Is this just a general comment about namespaces, or am I reading over it?

Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2012 Microsoft. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker