Fog Creek Software
Discussion Board

C# Class Library File Structure Opinion

I'm talking about laying out your c# class library here with respect to classes and files.

Do you guys feel like there's something wrong w/ laying out some 1:n relationships between <classes>:<physical_files> where relationsihps make sense. For example, I'm stuffing the following classes:

Employee : Person
Customer : Person

Into a Person.cs file.

I'm also stuffing the following classes:


Into a Contact.cs file.

Flame me!

Tuesday, April 6, 2004

Occasionally; but only if the subclasses (or superclass) is VERY small.  And/Or if the classes are tightly coupled.

Almost Anonymous
Tuesday, April 6, 2004

I tend towards a 1:1 class/file ratio. It just makes it easier to find what I'm looking for.

If a class requires extra types (like enum or delegate definitions) I'll put those in the same file with the class. If these definitions are needed in several classes, I haven't really decided where to put them yet. :-)

Chris Tavares
Tuesday, April 6, 2004

That sucks!  Just wait until you forget that you put the Employee class in the Person class file and you waste half an hour looking for it, discover it and wonder what moron did such a stupid thing.

obligitory flame
Tuesday, April 6, 2004

Is there a reason you've chosen to put multiple classes into a single .cs file?

I don't think there is anything wrong with doing it that way, it's just that it can make it confusing to find one and it can clutter up the file.

Mark Hoffman
Tuesday, April 6, 2004

I'm w/you.  I inherited a C# project with every class in it's own file.  Drives me nuts trying to find stuff.

I downloaded cygwin so I could use "grep" to find things -- that helped me a lot.

I guess this is encouraged by Visual Studio; my tendancy is more towards 1 file per assembly (DLL).  If it's all going into one library, why have 30 different source files.

Wednesday, April 7, 2004

"I'm w/you.  I inherited a C# project with every class in it's own file.  Drives me nuts trying to find stuff."

How hard is finding "SomeClass" in "SomeClass.cs"?

Certainly it's much easier than finding SomeOtherClass in "FooBar.cs" which also contains FooClass and BarClass.

Almost Anonymous
Wednesday, April 7, 2004

Finding a class is Visual Studio.NET is dead easy ... F12.

1:1 makes it even easier though ...

Wednesday, April 7, 2004

> Flame me!

Couldn't you put those classes in separate *.cs files, and wrap those classes into their own Contact.csproj assembly?

Christopher Wells
Wednesday, April 7, 2004

The primary benefit of a class per file is for source control and concurrent access: Why deal with the inevitable human error of code merges if each developer is really dealing with the separate classes.

Dennis Forbes
Wednesday, April 7, 2004

Dennis, very strong point . . . thank you.

Wednesday, April 7, 2004

And why not keep drilling down and do away with this 70's practice of relating source code to OS files? Let's do it the OO way, let's make it simpler...
Love the way Frans has put it

Alan Cyment
Monday, August 2, 2004

*  Recent Topics

*  Fog Creek Home