开发者

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 cl开发者_开发知识库eaner in terms of braces, but I don't know if it has the same properties.


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?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜