简体   繁体   English

将单个命名空间拆分到多个 C# 文件有什么好处?

[英]what's the benefits to split a single namespace across multiple C# files?

I'm reading a book which shows an example below:我正在阅读一本书,其中显示了以下示例:

Assume you are developing a collection of geometric classes named Square, Circle, and Hexagon.假设您正在开发一组名为 Square、Circle 和 Hexagon 的几何类。 Given their similarities, you would like to group them together into a unique namespace called MyShapes within the CustomNamespaces.exe assembly.鉴于它们的相似性,您希望将它们组合到 CustomNamespaces.exe 程序集中名为 MyShapes 的唯一命名空间中。 You have two basic approaches.您有两种基本方法。 First, you can choose to define all classes in a single C# file (ShapesLib.cs) as follows:首先,您可以选择在单个 C# 文件 (ShapesLib.cs) 中定义所有类,如下所示:

// ShapesLib.cs
using System;

namespace MyShapes
{
    public class Circle { /* Interesting members... */ }

    public class Hexagon { /* More interesting members... */ }

    public class Square { /* Even more interesting members... */ }
}

While the C# compiler has no problems with a single C# code file containing multiple types, this could be cumbersome when you want to reuse class definitions in new projects.虽然 C# 编译器对包含多种类型的单个 C# 代码文件没有任何问题,但当您想在新项目中重用 class 定义时,这可能会很麻烦。 For example, say you are building a new project and only need to use the Circle class.例如,假设您正在构建一个新项目,并且只需要使用 Circle class。 If all types are defined in a single code file, you are more or less stuck with the entire set.如果所有类型都定义在一个代码文件中,那么您或多或少会被整个集合所困扰。 Therefore, as an alternative, you can split a single namespace across multiple C# files.因此,作为替代方案,您可以跨多个 C# 文件拆分单个命名空间。

// Circle.cs
using System;
namespace MyShapes
{
    public class Circle { /* Interesting methods... */ }
}

// Hexagon.cs
using System;
namespace MyShapes
{
    public class Hexagon { /* More interesting methods... */ }
}

// Square.cs
using System;
namespace MyShapes
{
    public class Square { /* Even more interesting methods... */ }
}

I don't quite understand it, what does reuse class definitions in new projects mean?我不太明白,在新项目中重用 class 定义是什么意思? In both case, when you want to use Circle class in other projects, you need to use MyShapes.Circle c = new Circle() explicitly or use using MyShapes; Circle c = new Circle();在这两种情况下,当您想在其他项目中使用Circle class 时,您需要明确使用MyShapes.Circle c = new Circle()或使用using MyShapes; Circle c = new Circle(); using MyShapes; Circle c = new Circle(); , so there is really no difference between "define all classes in a single C# file' or "split a single namespace across multiple C# files"? ,所以“在单个 C# 文件中定义所有类”或“在多个 C# 文件中拆分单个命名空间”之间真的没有区别吗?

" you are more or less stuck with the entire set. Therefore, as an alternative, you can split a single namespace across multiple " 您或多或少地被整个集合所困扰。因此,作为替代方案,您可以将单个命名空间拆分为多个

The wording is suspect, and your intuitions are correct, you are still suck with the entire set.措辞是可疑的,你的直觉是正确的,你仍然对整个系列很烂。 They are the same thing, the only difference is you have class per file that share the same namespace (which some might say) can be easier to maintain in several ways such as readability, git commits and merges etc.它们是相同的东西,唯一的区别是每个文件都有 class 共享相同的命名空间(有些人可能会说)可以通过多种方式更容易维护,例如可读性、git 提交和合并等。

In any normal sense and directly relating to the authors comment " you are more or less stuck with the entire set ", this would only start to make a substantial difference when you had each shape in a different project / Nuget and you could selectively and granularly reference each shape, with the advantage that all shapes are still under the same archetype namespace.在任何正常意义上并且与作者评论直接相关“你或多或少地坚持整个集合”,当你在不同的项目/Nuget 中拥有每个形状时,这只会开始产生实质性的差异,你可以有选择地和细粒度地引用每个形状,其优点是所有形状仍位于相同的原型命名空间下。 Even then, shapes are not the best analogy.即便如此,形状也不是最好的类比。

Without getting bogged down in the semantics of the wording, the author is just implying you can split individual classes across files and have them still reside in the same namespace.在不拘泥于措辞语义的情况下,作者只是暗示您可以将各个类拆分到文件中并让它们仍然驻留在同一个命名空间中。

cumbersome when you want to reuse class definitions in new projects当您想在新项目中重用 class 定义时很麻烦

Basically, it is exactly what is written.基本上,这正是所写的。 If you have namespace for a project that contains a number of classes, if in the future you would like to reuse a class (why reinvent the wheel when you have already done the leg work - common in the professional setting), you would have to import the classes that you need.如果您有一个包含多个类的项目的命名空间,如果将来您想重用 class(为什么在已经完成腿部工作时重新发明轮子 - 在专业环境中很常见),您将不得不导入您需要的类。

But, if you have only one class that you needs to be imported (ie included in your project), but that class is in a file representing a number of classes - you will be importing all of the additional classes even though the project you are going to be working on has no need for them.但是,如果您只有一个 class 需要导入(即包含在您的项目中),但 class 位于代表多个类的文件中 - 您将导入所有其他类,即使您是项目将要工作不需要他们。 It is wasted memory.它浪费了 memory。

In addition, and this is not something that has been mentioned, it is a good practice to separate classes into their own files for easier readability and code management in the future.此外,这不是已经提到的,将类分离到自己的文件中是一个很好的做法,以便将来更容易阅读和代码管理。 Especially if your classes will grow to represent large objects that will have many methods, properties, etc. You would not want to have a 1000+ line file.特别是如果您的类将增长为表示将具有许多方法、属性等的大型对象。您不希望有一个 1000 多行的文件。

There are various good reasons for having one class = one file.拥有一个 class = 一个文件有很多充分的理由。

  1. you can copy the single.cs file in another project and with a little luck it will just work, and you won't import useless things (I say little luck because often a class uses other classes... For example all the shapes could have a class Shape as a base class... Then to copy Circle to another project you would have to copy both Circle and Shape )您可以将 single.cs 文件复制到另一个项目中,运气好的话它会正常工作,并且您不会导入无用的东西(我说运气不好,因为 class 经常使用其他类...例如,所有形状都可以有一个class Shape作为基础 class ... 然后将Circle复制到另一个项目,您必须同时复制CircleShape

  2. it is easy to find the.cs file that contains a class: it has the same name as the class.很容易找到包含 class 的 .cs 文件:它与 class 同名。 If a file contains multiple classes, what name should the file have?如果一个文件包含多个类,该文件应该有什么名称? It is quite easy after some time to have the filename become "obsolete" because you added some more classes一段时间后,文件名变得“过时”很容易,因为您添加了更多类

  3. it is quite easy to make a class that is already too much big as the number of lines... If you begin putting multiple classes in the same file this problem will only increase.制作一个已经太大的行数的 class 非常容易......如果您开始将多个类放在同一个文件中,这个问题只会增加。 But normally you don't have to check all the classes together... Perhaps today you have to work on Circle and tomorrow on Square .但通常你不必一起检查所有的课程......也许今天你必须在Circle上工作,明天在Square上工作。 It is quite useless to have both of them at the same time.同时拥有它们是非常没用的。 It would be like always carring around useless tools.这就像总是随身携带无用的工具。

  4. if you work with a VCS (Version Control System), like github, it is easy to see what files have been modified.如果您使用 VCS(版本控制系统),例如 github,则很容易看到哪些文件已被修改。 If one file = one class, you can have an idea about what has been modified.如果一个文件 = 一个 class,您可以了解修改了什么。 If multiple classes are inside a single file, everything becomes murkier如果多个类都在一个文件中,一切都会变得更加模糊

There are surely other good reasons... But the main good reason is: everyone is doing it... So probably it is the correct way.当然还有其他好的理由……但主要的好理由是:每个人都在这样做……所以这可能是正确的方法。 In at least a language (Java), it isn't a suggestion, it is a rule.至少在一种语言(Java)中,这不是建议,而是规则。 One file = one public class.一个文件 = 一个公共 class。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM