I build websites with c# and put the code of each single functionality into one .cs file. 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)?
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. 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. 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
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.
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.
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.
Direct link to Coding Standards
Follow some simple conventions like
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.