[英]c# multiple classes in separate files?
I just wondered what other peoples thoughts were regarding related classes in a single or separate .cs file? 我只是想知道其他人在单个或单独的.cs文件中关于相关类的想法是什么?
If, for example, I have an interface that is implemented by, say an arbitrary 10, other classes, would you place them all in the same file or separate them? 例如,如果我有一个由任意10个其他类实现的接口,你会将它们全部放在同一个文件中还是将它们分开?
Thanks. 谢谢。
I always go with separate files for each class. 我总是为每个班级分别使用单独的文件。 It's recommended best practice and it really makes sense.
这是推荐的最佳实践,它确实很有意义。
My approach is that 1 file == 1 class/interface/module/... whatever. 我的方法是1个文件== 1个类/接口/模块/ ...无论如何。 So the filename always reflects what's in there.
所以文件名总是反映出那里的内容。 To me that's the cleanest approach.
对我而言,这是最干净的方法。
I would separate classes into different files. 我会将类分成不同的文件。 This makes them a lot easier to find in the IDE.
这使得它们在IDE中更容易找到。
I would place each class in a separate file, and the interface in a separate file as well. 我会将每个类放在一个单独的文件中,并将接口放在一个单独的文件中。
I would give the file the following name .cs 我会给该文件以下名称.cs
That's a recommended best practice; 这是推荐的最佳做法; it allows you to find your classes very fast.
它可以让你快速找到你的课程。 I always go with this approach (except when I have inner classes offcourse. :) ).
我总是采用这种方法(除非我有内部课程。))。
I must agree with the rest here: 1 class = 1 file. 我必须同意其余部分:1 class = 1 file。
Also use correct namespacing for full project name as well as folders. 还要为正确的项目名称和文件夹使用正确的命名空间。 Interfaces also go into separate files, but I usually keep enums and structures inside other classes.
接口也进入单独的文件,但我通常将枚举和结构保存在其他类中。
Folders can be used to group certain classes together. 文件夹可用于将某些类组合在一起。 There is however a small issue when you "run out of names" so to speak.
然而,当你“没有名字”时可能会出现一个小问题。
Example: 例:
Solution: Tedd.CoolApp
解决方案:
Tedd.CoolApp
Project: Tedd.CoolApp.Engine
项目:
Tedd.CoolApp.Engine
Now what do I name the class? 现在我怎么命名这堂课? I want to name it
Engine
, but that would give me Tedd.CoolApp.Engine.Engine
... :) 我想把它命名为
Engine
,但那会给我Tedd.CoolApp.Engine.Engine
...... :)
The computer could care less about the folder structure you concoct, so this question definitely falls under the category of code readability. 计算机可能不太关心你编写的文件夹结构,所以这个问题肯定属于代码可读性的范畴。 As mentioned in this post about standards of code readability , friendly naming , consistency , and logical code separation are fundamental to the creation of readable code.
正如本文中提到的关于代码可读性标准 , 友好命名 , 一致性和逻辑代码分离是创建可读代码的基础。
So, where does that leave us? 那么,这让我们离开了什么地方? The creation of files--and the creation of namespaces and file regions--should be consistent.
文件的创建 - 以及命名空间和文件区域的创建 - 应该是一致的。 The names should be understandable.
这些名字应该是可以理解的。 And the code in each aggregate category should have something in common, as should be detailed in the category name.
并且每个聚合类别中的代码应该有一些共同点,应该在类别名称中详细说明。 Ultimately, with readability, you're considering that your code might be inherited by another poor fellow, and that the naming standards that you've created might help that poor fellow (a "tourist developer", if you will) more easily navigate around in the madness.
最后,在可读性的情况下,您正在考虑您的代码可能会被另一个可怜的家伙继承,并且您创建的命名标准可能会帮助那个可怜的家伙(“旅游开发者”,如果您愿意)更轻松地导航在疯狂中。
That's a lot of talking, so let me get down to brass tacks. 这是很多话题,所以让我来谈谈。 These are my rules, but I think they might be helpful to those who are looking to clean up their own code aquariums:
这些是我的规则,但我认为它们可能对那些想要清理自己的代码水族箱的人有所帮助:
namespace FatDish.Engines//.EngineExtensions { ...
namespace FatDish.Engines//.EngineExtensions { ...
When it comes to navigability, the first and second rule are key, as they directly aid in indicating to the "tourist developer" where any given piece of code resides. 当涉及到导航性时,第一和第二规则是关键,因为它们直接帮助向“旅游开发者”指示任何给定的代码片段所在的位置。
That's all I can think of at the moment. 这就是我现在所能想到的。 It's more important that you're consistent in your conventions than it is that you adopt any particular form of conventionality.
更重要的是,你的约定与你采用任何特定形式的惯例一致。 That will help other developers understand and consume your code at a quicker rate, and ensure that future developments in the project (written by folks other than yourself) stay within the same conventional, coherent bounds that you've established.
这将有助于其他开发人员更快地理解和使用您的代码,并确保项目的未来发展(由您自己以外的人编写)保持在您已建立的相同的传统,连贯的范围内。
Hope this helps! 希望这可以帮助!
Personally I adhere to Single Responsibility Principle where each of my classes has a single behaviour 我个人坚持单一责任原则,我的每个班级都有一个行为
think of a ecommerce site that has 想想有一个电子商务网站
I would separate these out to a User class, Billing Class and Orders class - the same would then adhere for an interface driven approach - 1 interface for each Responsibility 我将这些分离到User类,Billing Class和Orders类 - 然后遵循接口驱动的方法 - 每个责任的1个接口
check out SOLID design principles - each class would then be in owns own file and have a suitable naming convention to help 检查SOLID设计原则 - 每个类都将拥有自己的文件,并有一个合适的命名约定来帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.