Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# classes in separate files? [closed]

Tags:

c#

Should each class in my C# project get its own file (in your opinion)?

like image 348
Chris Avatar asked Sep 27 '08 23:09

Chris


2 Answers

While the one class per file policy is strictly enforced in Java, it's not required by C#. However, it's generally a good idea.

I typically break this rule if I have a very small helper class that is only used by the main class, but I prefer to do that as a nested inner class for clarity's sake.

You can however, split a single class into multiple files using the partial keyword. This is useful for separating your code from wizard-generated code.

like image 183
FlySwat Avatar answered Oct 10 '22 10:10

FlySwat


Files are cheap, you aren't doing anyone a favor by consolidating many classes into single files.

In Visual Studio, renaming the file in Solution Explorer will rename the class and all references to that class in your project. Even if you rarely use that feature, the cheapness of files and the ease of managing them mean the benefit is infinitely valuable, when divided by its cost.

like image 43
Chris Wenham Avatar answered Oct 10 '22 10:10

Chris Wenham