顶级通用类库的 Class.Class 与 Namespace.Class?

发布于 2024-08-24 20:15:08 字数 594 浏览 5 评论 0原文

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

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,代码大小是通过部分类来管理的,因此每个嵌套类可以位于单独的文件中,对于扩展方法也是如此(除了它们没有嵌套类)

我更喜欢 #2,有几个原因,例如能够使用已经常用的类型名称,例如 IO,我不想替换或碰撞。

你更喜欢哪一个?各自有什么优点和缺点吗?此案例的最佳实践是什么?

编辑:两者之间还会有性能差异吗?

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 ();

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)

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.

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

EDIT: Also would there be a performance difference between the two?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

樱娆 2024-08-31 20:15:09

我想说#1。因为当您将许多类捆绑到一个静态类中时,您会执行与命名空间相同的操作。这就是为什么我说最好让命名空间为你做这件事。

在这种情况下,您还可以通过在需要时添加 using 来摆脱在所有内容前面编写“NP”的麻烦。我认为你应该嵌套命名空间,这样它们就不会发生冲突,或者使用比 IO 更具描述性的命名空间名称。

大多数情况下,最佳实践是 Microsoft 所做的,但我从未见过他们这样做#2

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 meant for. Which is why I would say it is best to let namespaces do that for you.

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 or use more describing namespace names than IO.

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

尛丟丟 2024-08-31 20:15:09

命名空间的全部意义在于它们用于组织代码 :

命名空间在 C# 程序中以两种方式大量使用。首先,.NET Framework 类使用命名空间来组织其许多类。其次,声明您自己的命名空间可以帮助控制大型编程项目中的类和方法名称的范围。

当您查看 .NET 框架本身时,您会发现几乎所有内容的结构都与第一个示例(命名空间)类似,而几乎没有任何内容的结构与第二个示例(嵌套类型)类似。使用嵌套类型的极少数情况是,它们与外部类型紧密相连,本质上是外部类型的一部分,但由于代码重用等原因需要成为一个单独的类。

因此,如果您所说的“最佳实践”是指“将事物用于其设计目的”、“最像 .NET 框架”和“最接近 .NET 设计范例”,那么就没有争议了;使用命名空间进行组织,而不是嵌套类型。

关于您的编辑 - 不,在现实世界中不存在重要的性能差异。

The whole point of namespaces is that they are used to organise your code:

Namespaces are heavily used within C# programs in two ways. Firstly, the .NET Framework classes use namespaces to organize its many classes. Secondly, declaring your own namespaces can help control the scope of class and method names in larger programming projects.

When you look at the .NET framework itself, you can see that virtually everything is structured like your first example (namespaces), and virtually nothing is structured like your second example (nested types). The rare occasions that nested types are used are when they are so closely tied to the outer type that they are essentially part of it, but need to be a separate class for reasons such as code re-use.

So if by "best practice" you mean "using things for the purpose they were designed", "most like the .NET framework" and "corresponding most closely to .NET design paradigms" then there is no contest; use namespaces for organisation, not nested types.

Regarding your edit - no, there is no performance difference that will matter in the real world.

何必那么矫情 2024-08-31 20:15:09

我更喜欢#1,因为它不需要我通过一个类调用来到达另一个类。我认为这让事情有点混乱,因为一般来说,对象应该具有成员类、方法等,它们直接处理通过实例化类创建的对象。这意味着您并没有真正遵循 OOP 的原则。微软还表示#1 是最佳实践。

I prefer #1 because it does not require me to call through 1 class to get to another. I think it makes things a bit confusing because in general objects are meant to have member classes, methods, and such that deal directly with the objects made by instantiating the class. That means you are not really following the principles of OOP. Microsoft also says #1 is best practice.

相思故 2024-08-31 20:15:09

我从来没见过你用过2号。它让我想起了 VB6 模块。

I've never seen your number 2 used. It reminds me of VB6 Modules.

老旧海报 2024-08-31 20:15:09

我更喜欢#1,让命名空间来做。就像其他人所说的那样,调用类来访问其他类是非常令人困惑的。

此外,当您正在执行更复杂的操作(例如反射或使用提供程序模型之类的东西)时,将嵌套类作为您的实际提供程序部分或您尝试达到的目标会变得很麻烦。

最后,测试嵌套类和接口使测试变得更加困难。

I prefer #1 and let namespaces do it. Like others have said, it's very confusing to call into classes to get to other classes.

In addition, when you're doing more complex things like reflection or using something like a provider model, having nested classes being your actual provider section or the target you're attempting to reach becomes hairy.

Lastly, testing nested classes and interfaces make testing more difficult.

初心 2024-08-31 20:15:09

我认为您应该避免公开的(公共)嵌套类和接口,或者至少 Microsoft FxCop 是这么说的。因此,第一个更好。

编辑:(是的,改成第一个,当我累死的时候我不应该回复)

I think you should avoid exposed (public) nested classes and interfaces, or at least that is what Microsoft FxCop would say. Thus, the first one is better.

Edit: (yes, changed to the first one, i shouldn't reply in SO when i'm dead tired)

秋意浓 2024-08-31 20:15:09

事实上我发现自己有时会使用#2。我将在下一行之后解释原因。但对于空静态类的情况,命名空间更理想,因为这就是它们的用途。

但是,如果您有一个派生新类的用例,有时嵌套这些派生类可能是有意义的。

例如,您可以:

public abstract class Vehicle
{
    int NumberOfWheels;
}

public class SportsCar : Vehicle
{
}

现在,您可能希望将它们都放在 Vehicle 命名空间下,以便父类不会因所有不同的派生类而混乱。但是,这是一个 坏主意。

相反,嵌套它们可以给您带来所有好处:

public abstract class Vehicle
{
    int NumberOfWheels;

    public class SportsCar : Vehicle
    {
    }
}

I actually find myself using #2 sometimes. I'll get into why after the next line. But in your case with empty static classes, namespaces are more ideal because that is what they are for.

However, if you have an use case where you are deriving new classes, some times it may make sense to nest these derived classes.

For example, you could have:

public abstract class Vehicle
{
    int NumberOfWheels;
}

public class SportsCar : Vehicle
{
}

Now, you may want to put them both under Vehicle namespace so that the parent is not cluttered with all the different derived classes. However, that's a bad idea.

Instead, nesting them gives you all the benefits:

public abstract class Vehicle
{
    int NumberOfWheels;

    public class SportsCar : Vehicle
    {
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文