简体   繁体   English

c#单独文件中的多个类?

[英]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: 这些是我的规则,但我认为它们可能对那些想要清理自己的代码水族箱的人有所帮助:

  1. Place one class (or one interface, enum, or struct) in one file. 将一个类(或一个接口,枚举或结构)放在一个文件中。
  2. The name of the class should be the name of the file. 类的名称应该是文件的名称。
  3. Classes that inherit from the same base class should be in the same folder. 从同一基类继承的类应位于同一文件夹中。
  4. If at all possible, a class should be in the same folder as the interface that that class implements. 如果可能的话,类应该与该类实现的接口位于同一文件夹中。
  5. An interface should have the same name as the class, but should be prefixed with a capitalized "I". 接口应与类具有相同的名称,但应使用大写的“I”作为前缀。 It's the only bit of coding advice I still respect from the Hungarians . 这是我仍然尊重匈牙利人的唯一编码建议。
  6. The folder name should be a pluralized version of the base class. 文件夹名称应该是基类的复数版本。 For example, if we're creating a bunch of Engines , Engine should be the base class name, Engines should be the folder name, and all of the classes that inherit from Engine should be in the Engines folder. 例如,如果我们要创建一堆Engines ,则Engine应该是基类名, Engines应该是文件夹名,并且从Engine继承的所有类都应该在Engines文件夹中。
  7. The namespace structure should directly follow the folder structure. 命名空间结构应直接遵循文件夹结构。 So, the namespace for a given set of Engines (example from above) should be placed into a namespace called Engines . 因此,应将一组给定Engines的命名空间(上面的示例)放入名为Engines的命名空间中。 If Engines is a subfolder of a subfolder, each subfolder should be its own sub-namespace, eg Project1.Subfolder1.Subfolder2.Engines . 如果Engines是子文件夹的子文件夹,则每个子文件夹应该是其自己的子命名空间,例如Project1.Subfolder1.Subfolder2.Engines
  8. When you're dealing with partial classes that need to live in two separate folders (as one piece of the class is autogenerated), place the non-autogenerated class into a folder suffixed with Extensions . 当您处理需要存在于两个单独文件夹中的部分类时(因为该类的一部分是自动生成的),请将非自动生成的类放入以Extensions为后缀的文件夹中。 In the file, comment out the Extensions namespace like so: namespace FatDish.Engines//.EngineExtensions { ... 在文件中,注释掉Extensions命名空间,如下所示: 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 想想有一个电子商务网站

  • User Registration 用户注册
  • User Login 用户登录
  • billing 计费
  • Supplier Ordering 供应商订购

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.

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