Alex Rivera | Logout

How to organize C# classes

Asked 2010-08-05T20:07:13.240
19

Is there a general practice when it comes to how to organize your classes in C#? Should there only be one generic class per .cs file? I see that I have Form1.cs which includes all of the classes relevant to "Form1". However I could create a file named Misc.cs which includes all misc classes. Not sure which way to go so everything stays organized.

Or should I organize them a specific way? For example, I am accessing a MySQL database, so i'm creating a MySQL wrapper which I will store in MysqlWrapper.cs and name the class to match it. Should I create a new .cs for each class I create?

Or should I combine only the ones that use similar "using" namespaces such as System.Text; using System.Windows.Forms; etc?

c#
Edit
Report

2 Answers

2

I don't strictly follow a one-class-per-file rule. If I'm dealing with a very large class with lots of code, I might give it its own file,. Generally, though, I don't have a problem with keeping multiple classes in the same file as long as they are related to one another in a logical way. This tends to work best with small classes, but may not be the best idea for large ones.

answered 2010-08-05T20:10:06.930
0

I normally have one class per file. The only exception to that might be if a class has a private nested class and I would normally keep that within the same file rather than splitting it out to a different one using the partial keyword. This way I find things easier to locate.

answered 2010-08-05T20:10:51.347

Your Answer