Search code examples
c#namespacescode-organization

Multiple levels of namespaces


In C#, you can do:

namespace Blah
{
    namespace Foo
    {
    }
}

Or:

namespace Blah.Foo
{
}

Which should I prefer, if either? The bottom one is somewhat cleaner in terms of braces, but I don't know if it has the same properties.


Solution

  • Both behave equally, and I strongly prefer the second one.

    Usually, people prefer having 1 .cs file per class/interface, or at least for groups of similar ones (e.g., all Tuple implementations), which means you usually have 1 namespace in a .cs file anyway.

    Nested Namespaces add levels of indentation. Usually, you are already 3 levels deep (namespace, class, method) for each piece of code that you write, so why add even more unnecessary indentations?