Should enums in C# have their own file? [closed]

2019-01-31 05:02发布

I have a class which uses an enumeration, the enum is currently in its own file which seems wasteful.

What is the general opinion on enums being placed within the namespace of a file that they are consumed in? Or should the enum really live in its own cs file?

Edit

I should mention that while the class in question uses these enumerations, so does external callers. In other words, another class can set these enumerations. So they are not used internally to the class, otherwise this question would be a no brainer.

15条回答
太酷不给撩
2楼-- · 2019-01-31 05:38

I like to have one public enums file named E containing each seperate enum, then any enum can be accessed with E... and they are in one place to manage.

查看更多
欢心
3楼-- · 2019-01-31 05:39

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.

查看更多
Rolldiameter
4楼-- · 2019-01-31 05:39

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.

查看更多
登录 后发表回答