.NET Framework - Multiple classes in one file?

Asked By Davej on 04-Apr-12 01:52 PM
In what languages in this considered acceptable or even good form?

Gene Wirchenko replied to Davej on 04-Apr-12 06:00 PM
It can be in ones that do not tie a class to a file.

I would rather group a number of similar, small classes together.


Gene Wirchenko
Arne_Vajhøj replied to Davej on 04-Apr-12 07:51 PM
This is a C# group so I assume that your most interested in C#.

I believe that it is very common to just have one class
per file for C#.

Personally I tend to put a small number of closely
related classes in the same file. I consider that
acceptable and preferable. It obviously assumes
that the structure is so logical that going from
class name to file name is always easy.

For other languages, then C++ typical have multiple
classes per file and Java rarely have multiple classes
per file.

Marcel_Müller replied to Davej on 05-Apr-12 06:41 AM
There are languages where this is not permitted. However, in C# you can
spread the code over files almost as much as you like. Neither the class
mane needs to match the file name nor the nemespace needs to match the
path nor a class has to be defined in a single file (partial classes)
nor one file could contain only one class. It is only a question of code
style rules.

From my point of view the code should primarily be readable. And
whatever is required to meet this demand should be done. Strict coding
rules usually do not always hit the nail on the head. Sometime it is a
good advise to spread a class over files. First of all in case of
generated code (which itself is a work-around for some other problem).
Secondly a nested class might be better placed in a separate file if it
is rather large. On the other hand small classes or interfaces or enums
that are dedicated to a single class only, might be naturally placed in
the class file without causing any confusion.

Davej replied to Marcel_Müller on 05-Apr-12 08:36 PM
Thanks for the responses. I guess what had me confused is that it
seems to be a hard requirement in Java and a recommended practice in
C#. I am learning both of these concurrently and get them confused
Arne_Vajhøj replied to Davej on 05-Apr-12 09:20 PM
Java and C# are quite different in this regard.

Java requires public outer classes to be in separate
files if a file system is used to store source code
(and it is more than 10 years since I last saw something
other than a file system used) and having the directory
structure match the package structure.

C# let you choose how you want to do it.


p1/p2/C1.java with p1.p2.C1
p1/p2/C2.java with p1.p2.C2
p1/p3/C3.java with p1.p2.C3
p1/p3/C4.java with p1.p2.C4

can be done in multiple ways in C#.

Like Java:

NS1/NS2/C1.cs with NS1.NS2.C1
NS1/NS2/C2.cs with NS1.NS2.C2
NS1/NS3/C3.cs with NS1.NS2.C3
NS1/NS3/C4.cs with NS1.NS2.C4

or more compact:

NS1/NS2.cs with NS1.NS2.C1 and NS1.NS2.C2
NS1/NS3.cs with NS1.NS2.C3 and NS1.NS2.C4

If working for a company then follow the established

If just you then do whatever you think is easiest
to work with.