Multiple Levels of Namespaces

In C # you can do:

namespace Blah
{
    namespace Foo
    {
    }
}

Or:

namespace Blah.Foo
{
}

What should I prefer if? The bottom is a little cleaner in terms of brackets, but I don’t know if it has the same properties.

+3
source share
3 answers

Both behave the same, and I prefer the second option.

Usually people prefer to have a 1.cs file for each class / interface, or at least for groups of similar ones (for example, all Tuple implementations), which means that you usually have 1 namespace in any .cs file.

Nested namespaces add indentation levels. As a rule, you are already at 3 levels (namespace, class, method) for each part of the code that you write, so why add even more unnecessary deepenings?

+2

. (, -) Blah ( ). , - ... .

+2

Two methods give the same results: the code inside will be in the namespace Blah.Foo.

I would prefer the second version, because it is shorter and does not unnecessarily add an indentation level.

I could see the first option used if you want to declare several types from related namespaces in one file, but I think this is not a good idea in most cases.

+2
source

All Articles