34

Possible Duplicate:
Is it a bad practice to have multiple classes in the same file?

Is it advisable to create multiple classes within a .cs file or should each .cs file have an individual class?(Class file name also Animal.cs)

public class Animal
{
}

public class Person
{
}

public class Utility
{
}
Community
  • 1
  • 1
Learner
  • 513
  • 2
  • 9
  • 17
  • Its better to keep a single file for each class, with respect to Source Control, IMO – Habib Aug 02 '12 at 05:00
  • 4
    In case of HUGE applications, IF I have A LOT of very small structs, I prefer to make single file with ALL of them. For example, VectorTypes.h/cpp, CommonTypes.h/cpp, and I usually apply this to all languages that support it. I have situations where 4 related small classes end up in single file while single huge class gets divided into one header and 4 source files. – Петър Петров Mar 05 '15 at 21:44

5 Answers5

26

Simply say good practice is one class from one file. BUT if your application is quite small you can use several classes in one file.

shashwat
  • 7,851
  • 9
  • 57
  • 90
TechGuy
  • 4,298
  • 15
  • 56
  • 87
  • 6
    If anyone doing a coding interview, please make it a separated file. This is just my 2cents from experience. – Jin Lim Oct 18 '19 at 12:12
21

When designing classes we respect the Single Responsibility Principle. Reading code becomes a lot easier if its shape follows its semantics, hence splitting files by class is sensible.

However if there are inner classes it makes sense to keep them in the same file

Source: Old Post

Community
  • 1
  • 1
Vinoth
  • 2,419
  • 2
  • 19
  • 34
14

For the sake of making things simple, you should probably put them in different files. I can't think of any advantages of keeping them in the same file.

Paolo del Mundo
  • 2,121
  • 13
  • 18
  • I ask you is this correct approach ? – Learner Aug 02 '12 at 04:31
  • 2
    As Paolo said,there is no benefit keeping many classes in a single file. Putting them in separate files will help you and any other programmer to find the cs file for a class quickly. – ZafarYousafi Aug 02 '12 at 04:37
  • 1
    By all that is right in the world, yes, it is correct! Try and find the code file to review for the "Person" class when you are debugging a major production deployment that just dumped. Which file would you look for? – GalacticJello Aug 02 '12 at 04:38
2

It depends about your application. If it is a big application, I think that isn't good. Data must be very organized.

I think that is better to keep them in different files.

But, if you are working for a small application, it is good to keep them in same file.

Hope that I helped you.

Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474
  • Can you add a reason why it is good to keep them in the same file? – GalacticJello Aug 02 '12 at 04:39
  • For very small applications It doesn't need to create many files. This is my opinion. Like I said, I recommend to keep them in different files. 1 class/file. Peace. – Ionică Bizău Aug 02 '12 at 04:41
  • 1
    In case of HUGE applications, if I have A LOT of very small structs, I prefer to make single file with ALL of them. For example, VectorTypes.h/cpp, CommonTypes.h/cpp, and I usually apply this to all languages that support it. I have situations where 4 related small classes end up in single file while the opposite - single huge class - gets divided into one header and 4 source files. It's about organization of the code. If I need a big class with 2 smaller helper mini-classes, I will for sure make them end up in single compilation unit – Петър Петров Mar 05 '15 at 21:46
2

Logically its depend on your scope of application. But Normally best practice to do coding in seperate class (New File , new class )

Splittting files will be more sensible.Because industries follow ths standards...

Niks
  • 997
  • 2
  • 10
  • 28