In C# projects, we frequently group small and tightly related classes into the same
.cs file. This practice reduces the friction of dealing with a myriad of files containing close to no actual code. Yet, is there an established practice to name the file that contains multiple classes?
My advice: avoid files containing multiple classes and name files==classes even if you mean there are too many files. Try to organize them in folders. In very special cases you might have nested classes. In this case it makes sense separating the main class and a nested class into different files having one partial class. In this case I use following naming convention.
Probably you can also use multipart file names in your case.
Try to avoid having more than one class in a file, not more than one public class atleast. While naming class files with multiple classes, choose a descriptive filename.
- http://weblogs.asp.net/lhunt/pages/CSharp-Coding-Standards-document.aspx. (2.2 Name Usage & Syntax)
- http://blogs.msdn.com/b/brada/archive/2005/01/26/361363.aspx (2.8 File Organization)
Consider that by asking a question about how to name such files, you have already identified a code smell that is problematic for you, and this is a problem that will not go away easily.
Regardless of the reasoning, you'll find that it is difficult to choose sensible names when you have more than one class in a file, because even if the files are related in some way, there will always be the temptation to throw in something that doesn't quite fit the group name that you assign to a set of classes.
The short answer therefore is that there is no recognized or established standard for naming these sorts of files. My suggestion would be to sit down with your team and look to the organization of your code, and apply a bit of a refactoring effort to tidy things up a bit.
However, to make things a little easier to begin with I'd ask if the additional classes are nested, or independent. If independent, then these classes can be more easily split out into their own files, with each file named for the single class contained within. If nested, then further decisions may need to be made about how such classes should be organised, however the nested classes may not need to be removed immediately, and the files would therefore be named for the outermost class. As far as nesting is concerned, there are lots of arguments against, and a few reasonable ones for nesting under some circumstances, and my advice would be to ask if sensible namespacing and creating a more organized source code structure might be a better way to tackle the problem.
Realistically however, you will find that by the end of the lifetime of your project, you will have wished your files all had contained only a single class and a nice directory/namespace structure to go with it.
I used to be all for having multiple classes in one file, but ever since I started actually working as a programmer (rather than being on my own), I have found that it can be a maintenance nightmare with many classes in a single file. Although, admittedly, Visual Studio can help a lot by using F12 (go to definition).
What I have started doing is using a naming convention as follows: namespace.classname.cs That way I know exactly what is in each file and it also provides some sort of overall context of the file. If the class is going to be in the default namespace, then I go ahead and just use the classname.cs (similar to Java).