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

后端 未结 15 2233
眼角桃花
眼角桃花 2020-12-08 03:48

I used to have one class for one file. For example car.cs has the class car. But as I program more classes, I would like to add them to the same file. For

15条回答
  •  死守一世寂寞
    2020-12-08 04:04

    You should refrain from doing so, unless you have a good reason.

    One file with several small related classes can be more readable than several files. For example, when using 'case classes', to simulate union types, there is a strong relationship between each class. Using the same file for multiple classes has the advantage of grouping them together visually for the reader.

    In your case, a car and a door do not seem related at all, and finding the door class in the car.cs file would be unexpected, so don't.

提交回复
热议问题