20

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?

4
  • 14
    frankly I prefer one file one class even for very small classes Nov 23, 2011 at 8:44
  • You can choose to name it as per the group these tightly related classes fall under.
    – V4Vendetta
    Nov 23, 2011 at 8:47
  • Maybe those classes ARE too small? Personally I only group multiple enums in one file (sometimes with the class, sometimes all enums together, depending on the context). Nov 23, 2011 at 8:55
  • @Felice: look at MSpec :) Apr 20, 2012 at 16:45

4 Answers 4

19

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.

MyEnumerable.cs
MyEnumerable.MyEnumerator.cs

Probably you can also use multipart file names in your case.

1
  • 4
    One more idea. You can group multiple .cs files inside Visual Studio Project so that they all appear under the same node in tree (like .Designer.cs files). This will signal you that classes tightly belong together.
    – achitaka-san
    Nov 23, 2011 at 8:58
6

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.

Refrences:

0
4

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.

Cheers,

3

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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.