coding style - Should enums in C# have their own file?

ID : 20252

viewed : 7

Tags : c#coding-styleenumsc#

Top 5 Answer for coding style - Should enums in C# have their own file?

vote vote


I wouldn't say "wasteful" (how much does an extra file cost?), but it is often inconventient. Usually there's one class that's most closely associtated with the enum, and I put them in the same file.

vote vote


This is really just a matter of preference.

I prefer to put each enumeration in its own file (likewise for each interface, class, and struct, no matter how small). It makes them easier to find when I'm coming from another solution or otherwise don't already have a reference to the type in question.

Putting a single type in each file also makes it easier to identify changes in source control systems without diffing.

vote vote


This is entirely a matter of style. What I tend to do is to have a file called Enums.cs in the solution in which the enum declarations are collected.

But they are typically found through the F12 key anyway.

vote vote


The question to ask yourself would be: is there anything about an enumeration type in C# that indicates I should treat it differently from all other types I create?

If the enumeration is public, it should be treated like any other public type. If it is private, declare it as a nested member of the class using it. There is no compelling reason to put two public types in the same file simply because one is an enumeration. The fact that it is a public type is all that matters; the flavor of type does not.

vote vote


Another advantage of putting each type (class, struct, enum) in its own file is source control. You can easily get the entire history of the type.

Top 3 video Explaining coding style - Should enums in C# have their own file?