3

Possible Duplicate:
C# classes in separate files?

I came to a new company and found that a C# project had in it a single source file.., that had within it two interfaces and two public classes. (Truth is this is how they do things in most of their projects.) Neither of the three classes were nested. They did however have a chain of inheritance.., starting with on of the two classes using dependency injection.

Question 1: So my question really is NOT regarding the pattern as much as what is good practice regarding whether or not each of these interfaces should be in on single source file or a source file per class or interface?
Question 2: Is there a good blog, forum, link, or book addressing this issue?

I don't instantly like this kind of practice with a single source file being used. I'm use to single a source file per class or interface. I like a source file per class or interface because it gives me file level browse regarding encapsulation possibilities. Also the name of the file starts with ISomething and then has two public classes in it. :(

Community
  • 1
  • 1
apolfj
  • 940
  • 3
  • 14
  • 27
  • ISomething.cs is a really bad name for the file if that's what it contains. What is the access modifer on the interface? Personally, I always do 1 Interface/1 class / 1 enum per .cs file – Stealth Rabbi Sep 30 '11 at 17:46
  • Similar Question Where you can find some information here where he wants to now why using nested classes [here][1] [1]: http://stackoverflow.com/questions/48872/why-when-should-you-use-nested-classes-in-net-or-shouldnt-you – Nivid Dholakia Sep 30 '11 at 17:47
  • I am not asking about nested clases. I am however asking about non-nested classes in the same source file. – apolfj Sep 30 '11 at 17:50
  • I used ISomething as pseudo name. Again my question is not surrounding this specific detail. The main idea I tried to convey here was that the file name started with I Then Whatever Following that but had two public classes inside of it that were not interfaces. – apolfj Sep 30 '11 at 17:54

1 Answers1

2

It is a bad practice only if it violates name-matches-content rule. So if you have Foo.cs and inside you got class Foo and Bar, then that would be a problem. However it is totally cool to have to have Vehicles.cs with classes Car and Truck inside.

This is just my personal rule that served me well.

Ilia G
  • 10,043
  • 2
  • 40
  • 59