Class.Class VS Namespace.Class的顶级通用类库?类库、Class、VS、Namespace

2023-09-03 04:09:36 作者:百无一用是痴情

哪一个更可接受的(最佳实践):

Which one is more acceptable (best-practice)?:

namespace NP
   public static class IO
   public static class Xml
   ...
   // extension methods

using NP;

IO.GetAvailableResources ();

VS

public static class NP
   public static class IO
   public static class Xml
   ...
   // extension methods

NP.IO.GetAvailableResources ();

也为 2 中,code尺寸是通过让部分类管理,以便每个嵌套类可以是一个单独的文件,相同的扩展方法(除没有嵌套类为他们)

Also for #2, the code size is managed by having partial classes so each nested class can be in a separate file, same for extension methods (except that there is no nested class for them)

我preFER 2 ,为一对夫妇的原因,喜欢的是可以使用已经普遍使用,如 IO类型名称,我不希望更换或碰撞。

I prefer #2, for a couple of reasons like being able to use type names that are already commonly used, like IO, that I don't want to replace or collide.

哪一个你preFER?任何优点和缺点每个?什么是这种情况的最佳做法是什么?

Which one do you prefer? Any pros and cons for each? What's the best practice for this case?

编辑:也会有两者之间的性能差异

Also would there be a performance difference between the two?

推荐答案

我想说的#1。因为当你捆绑了大量的类到一个静态类,你做同样的事情作为一个命名空间是换货的。这就是为什么我会说这是最好的让命名空间为你做的。

I would say #1. Because when you bundle up lots of classes into one static class you do the same thing as a namespace is ment for. Which is why I would say it is best to let namespaces do that for you.

在这种情况下,你也可以摆脱其用在你想加入的情况下写的NP眼前的一切的。我觉得就应该把你的巢命名空间,使他们不会相撞我们使用更多的描述比IO命名空间的名称。

In that case you can also get rid of having to write "NP" in front of everything by adding using in case you want. I think you should either nest your namespaces so that they wont collide our use more describing namespace names than IO.

大多数时候,最好的做法是微软做的,我从来没有见过他们做的#2

Most often the best practice is what Microsoft do and I have never seen them do #2