如何对类中的枚举、结构体和子类进行分组?

发布于 2024-09-14 23:40:54 字数 189 浏览 5 评论 0原文

我听说过关于过度使用区域的不好的事情,但是当向我的类添加枚举时,我将它们放入类末尾的 #region "Enums" 中,并对结构甚至子类执行相同的操作。

是否有更好/标准的方法来将这些元素分组到类中?

(注意:这被标记为 C#/VB,但其他语言(例如 Java)可能也存在相同的情况)

I've heard bad things about overusing regions but when adding enums to my classes I put them into a #region "Enums" at the end of the class, and do the same with structures and even subclasses.

Is there a better/standard way to go about grouping such elements on classes?

(Note: this is tagged C#/VB but maybe the same situation exists for other languages, like Java)

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

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

发布评论

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

评论(1

无所的.畏惧 2024-09-21 23:40:54

当然,合理的分组是使用单独的源文件。

对于超过几个屏幕的类,您可能应该为此类类提供一个单独的源文件。否则,在源文件中查找各种类和其他项目可能会很困难。

对于语义相关且每个枚举都足够小以适合屏幕的枚举集合,将它们放入单个文件中通常是有意义的 - 但您应该根据所有这些枚举共享的目的来命名该文件。如果你很难想出一个深刻的名字来描述它们的共性,那么这可能表明你应该再次将它们分开。 (当然,本段同样适用于类和结构,但仅适用于非常小的类和结构。)

请注意,即使是嵌套的类/结构/枚举也可以放在 C# 中的单独源文件中 - 只需声明外部类为部分

如果您有相关派生类(子类)的整个类层次结构,您甚至可以为每个此类层次结构创建一个子目录。例如,在名为 Collections 的子目录中,您可能有一个名为 CollectionBase.cs 的抽象基类文件和每个派生类的单独源文件。

Surely a reasonable grouping is to use separate source files.

For classes that are more than a few screens full, you should probably have a separate source file for such a class. Otherwise finding the various classes and other items within a source file can be difficult.

For a collection of enums which are semantically related and which are each small enough to fit on a screen, it often makes sense to put them into a single file — but then you should name the file according to a purpose that all of those enums share. If you have trouble coming up with a poignant name that describes their commonality, then this may be an indication that you should split them up again. (Of course this paragraph applies equally to classes and structs, but only very small ones.)

Notice that even nested classes/structs/enums can be placed in a separate source file in C# — just declare the outer class as partial.

If you have a whole class hierarchy of related derived classes (subclasses), you could even create a subdirectory for each such hierarchy. For example, in a subdirectory called Collections, you might have a file called CollectionBase.cs for the abstract base class and a separate source file for each derived class.

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