简体   繁体   English

如何使用.cs文件和类来组织C#代码?

[英]How should I organize my C# code by using .cs files and classes?

I build websites with c# and put the code of each single functionality into one .cs file. 我使用c#构建网站,并将每个功能的代码放入一个.cs文件中。 However, the code of some functionalities can grow really large and become very hard to manage and debgug. 但是,某些功能的代码可能会变得很大,并且变得很难管理和调试。 To better manage the code in this kind of .cs files, should I sub-devide this .cs file into multiple .cs files (will this cause problem of the solution tree structure not as clear as before. Multi cs files only fulfill one same functionality) or should I create some inline classes (ie, still in the same one single .cs file, but contains seveal classes)? 为了更好地管理此类.cs文件中的代码,我应该将该.cs文件细分为多个.cs文件(这会导致解决方案树结构的问题不像以前那样清晰。多个cs文件只能满足一个相同的要求功能)还是应该创建一些内联类(即,仍在同一个.cs文件中,但包含多个类)?

It seems that I don't have enough knowledge about how to well organize the code in my project by using .cs files and classes. 似乎我对如何使用.cs文件和类很好地组织项目中的代码没有足够的了解。 Is there any standards or guidelines that I should follow? 我应该遵循什么标准或指南? Is there any book or ducuments can be recommended? 有什么书或书档可以推荐吗?

Many thanks for your input! 非常感谢您的输入!

Wei

You should follow the convention of one class per file. 您应该遵循每个文件一个类的约定。 There are some variations; 有一些变化。 a partial class may be split among multiple files. 局部类可以拆分为多个文件。 Nested classes will be contained within the containing classes file. 嵌套的类将包含在包含的类文件中。

If your following this and your files are still becoming large, it's likely that you're putting too much functionality within a class. 如果您遵循此步骤并且文件仍然很大,则可能是您在类中添加了太多功能。 A class should exist to satisfy a (single) behavior (known as Single Responsibility principal), and have the necessary state to carry out that behavior. 应该存在一个类来满足(单个)行为(称为“单一职责”主体),并具有执行该行为的必要状态。 Modifying behavior can be done in a variety of ways; 可以通过多种方式来修改行为。 inheritance, composition, etc. 继承,组成等

There is not general rule which applies to all use cases, but a good starting point is to have one file per class. 没有适用于所有用例的通用规则,但是一个好的起点是每个类只有一个文件。 Obviously you should give class and file the same name. 显然,您应该给class和file相同的名称。 Regarding your problem, I would propose to focus on your code first. 关于您的问题,我建议首先关注您的代码。 If a single class/file grows to large, there is probably a problem with your code. 如果单个类/文件变大,则代码可能存在问题。 Have a look at these links: 看一下这些链接:

http://en.wikipedia.org/wiki/Single_responsibility_principle http://en.wikipedia.org/wiki/Single_responsibility_principle

http://en.wikipedia.org/wiki/Don%27t_repeat_yourself http://en.wikipedia.org/wiki/Don%27t_repeat_yourself

Is better to create one .cs file for each class into your business logic, it does give no problem to you for the development, only if you have them in the same namespace, if you place them in different namespaces you only have to use using wordkey to take the information of different classes into the namespace you imported. 最好为您的业务逻辑中的每个类创建一个.cs文件,对于开发来说,这确实没有问题,仅当您将它们放在相同的名称空间中时,如果将它们放在不同的名称空间中时,您才需要使用wordkey将不同类的信息带入您导入的名称空间。

As an example, you can have the different files: 例如,您可以拥有不同的文件:

Car.cs
Person.cs
Main.cs

and, inside Main.cs, you can use the classes and public members inside the other two files. 并且,在Main.cs中,您可以在其他两个文件中使用类和公共成员。

Is a best practice to write classes implementation in separate files from others and this gives no problems. 最好的做法是在彼此不同的文件中编写类实现,这没有任何问题。

I hope this could be usefull for you. 我希望这对您有用。

See you. 再见。

For a general convention which works well and will give you some ideas, have a look at the C# coding standards docuement on the iDesign website. 有关行之有效的常规约定,并会给您一些想法, 访问iDesign网站上的C#编码标准文档。

Direct link to Coding Standards 直接链接到编码标准

Follow some simple conventions like 遵循一些简单的约定,例如

  1. One Page One Class 一页一页
  2. Create Some your own conventions (or you may use some existing ) for Declaring Functions and Properties. 创建一些您自己的约定(或者您可以使用一些现有约定)来声明函数和属性。
  3. Always Make Use of Regions to separate out logic. 始终利用区域来分离逻辑。
  4. Always Isolate EventHandlers, Functions, Properties from each other. 始终将事件处理程序,函数,属性相互隔离。 For example put all properties at the top and surround them with Region then Functions and then Event handlers like that.. 例如,将所有属性放在顶部,然后将它们包围在“区域”,“函数”和“事件处理程序”中。
  5. Always avoid duplication, or we can say Redundancy, means code written once should not be repeated again. 始终避免重复,或者我们可以说冗余,这意味着一次编写的代码不应再次重复。 (For that make use of concepts like polymorphism, Functions, Inheritance) (为此,利用了多态,函数,继承等概念)
  6. Make things in classes private unless you require them somewhere else. 除非您在其他地方有要求,否则将类中的内容设为私有。
  7. Whatever functions you make make them generic. 无论您使什么功能使它们通用。

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

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