Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Which is better: many class definitions in same file or every class definition in separate file?

Examples:

1) Many classes in same file. Say, myManyClasses.cs:

namespace myPack
{
   class myClass1()
   {
   }
   class myClass2()
   {
   }
   class myClass3()
   {
   }
  . 
  . 
  .
}

2) Every class in separate file:

myClass1.cs

namespace myPack
{
   class myClass1()
   {
   }
}

myClass2.cs

 namespace myPack
 {
   class myClass2()
   {
   } 
 }

. . .

share|improve this question

migrated from stackoverflow.com Mar 11 '11 at 19:17

This question came from our site for professional and enthusiast programmers.

    
    
@js1568: How did you find these questions? I search but not found. –  Javed Akram Mar 11 '11 at 18:20
    
What would StyleCop tell you? –  Job Mar 12 '11 at 18:49

9 Answers 9

Every class in a different page of course. Unless the class is ridiculously small or related only to one class that is the main class in the file.

share|improve this answer
    
care to give reason? –  Louis Rhys Sep 12 '12 at 2:13
    
So you can just use the file name to see / open the class and you don't need to remember where was that class. Although in visual studio its become super easy to quickly find / open a class definition but still its a bit messy when you have a file open and see a large set of "unrelated" classes and wonder why they are in the same file. –  basarat Sep 12 '12 at 16:11

I keep each class in a single file because

  • it makes it easier to navigate in the Solution Explorer
  • it offers a better granularity when working with version control systems, especially those where you have a locking checkout mechanism for editing files; you need to lock only the code for the class you are editing.
  • it follows the most common convention, making it easier for new developers to find their way around

The only exception I make to this rule is that I sometimes put enums into a a common Enums.cs file, since they typically are super small, rarely updated and contain no logic. I used to do the same with delegates, but since the generic Func and Action delegates came along, it's extremely rare that I define my own delegates.

share|improve this answer

This question fits better on http://programmers.stackexchange.com/

To answer it anyway, most programmers seem to agree that one class per file is the way to go. I happen to agree.

If you have multiple classes in one file, you are just making it harder to find anything in that file.

share|improve this answer

One class per page is better, but sometimes where tha class is very small and only used inside the same class file, well, i think is self explanatory.

share|improve this answer

My classes rarely stay simply enough to put many in the same file, so generally, I would go with the classes in many files approach.

share|improve this answer

I keep in one file one class or interface it is much easy to find and match class by file name I use the ReSharper wich helps me to find and navigate classes

share|improve this answer

I much prefer having one class per file.

The reason is that I can locate the class files more easily in Solution Explorer. I don't have to look at the class view to find the class.

Also, as your application grows, it is likely that the classes will grow as well. They will get more properties and methods. What started out as a small class could evolve into one that is quite large. At that point, it would be awkward to move the class code into a separate file.

share|improve this answer
2  
if your classes grow together with your application they probably violate the Single Responsability Principle –  stijn Mar 11 '11 at 19:57

One class per file.

It has many pros in Visual Studio. For example, if you change file name, it'll prompt you to automatically change class name.

Anyway, because we're in a programming era where refactoring is a everyday's task, a good naming convention and an assembly member per file would make things easier to handle if you've to improve, change or revamp your source code. And it can be less confusing for your project development newbies!

share|improve this answer

I prefer to structure my code to avoid cyclical dependencies between source files. In general, I put a single class in a source file, but if I have two or more tightly coupled classes, I will collect them in a single file. However, in that case I would be strongly inclined use inner classes if possible.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.