How many classes should be in one .cs file and why?

Experience Level: Junior
Tags: C#

Answer

In one .cs file there should ideally be just one class and the file should have the same name as the class. This is a recommended practice as the .cs files are then small enough to be maintainable. When more people work on the application there is a lower chance that two people will be modifying the same class and will be clashing. Also when you see the file name, you immediately know what class is in the file just by looking at its name without the need to open the file.

Following the rule one class per one file your code is more maintainable, less time is spent by searching, the risk of having conflicting changes in one file is decreased.

Comments

No Comments Yet.
Be the first to tell us what you think.
C# for beginners
C# for beginners

Are you learning C# ? Try our test we designed to help you progress faster.

Test yourself