PrettyPrincessKitty FS
PrettyPrincessKitty FS

Reputation: 6400

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.

Upvotes: 3

Views: 2369

Answers (3)

svick
svick

Reputation: 245046

The two ways produce identical results: code inside is going to be in the namespace Blah.Foo.

I would prefer the second version, because it's shorter and doesn't unnecessarily add a level of indentation.

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

Upvotes: 2

Marc Gravell
Marc Gravell

Reputation: 1064204

They are identical. The only difference is the ability to put extra things (types, using-directives) just inside the outer Blah in the top example (which may or may not be a good idea). I don't think I've ever used the first version in real code... The second version is preferred, usually.

Upvotes: 2

Michael Stum
Michael Stum

Reputation: 181104

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?

Upvotes: 2

Related Questions