Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to organize C# classes

Tags:

c#

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?

like image 454
muncherelli Avatar asked Aug 05 '10 20:08

muncherelli


People also ask

What are the two main ways a file can be organized in C?

Writing to a file (fprintf or fputs)

What is file structure C?

A FILE is a type of structure typedef as FILE. It is considered as opaque data type as its implementation is hidden. We don't know what constitutes the type, we only use pointer to the type and library knows the internal of the type and can use the data. Definition of FILE is in stdio although it is system specific.


1 Answers

Edit - this answer is meant to supplement the good answers that others have already posted.

Everyone else seems to be answering specifics. I'm thinking you're going to have more "best practices" design questions.

The official guidelines can be found here: http://msdn.microsoft.com/en-us/library/czefa0ke(VS.71).aspx

In particular, dig into the Naming guidelines, and then into the Namespace Naming guidelines and Class Naming guidelines.

And, as others have mentioned, please, one class per file. It makes things easier on the poor maintenance developer who will follow you.

like image 111
David Avatar answered Sep 21 '22 01:09

David