Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is the best place to locate enum types?

Tags:

enums

I have found that there is generally a singe type or namespace that takes in any particular enum as a parameter and as a result I have always defined those enums there. Recently though, I had a co-worker make a big deal about how that was a stupid thing to do, and you should always have an enum namespace at the root of your project where you define everyone of your enum types.

Where is the best place to locate enum types?

like image 584
aceinthehole Avatar asked Oct 17 '08 20:10

aceinthehole


People also ask

Where should enum be placed?

Put the enums in the namespace where they most logically belong. (And if it's appropriate, yes, nest them in a class.)

Where are enum variables stored?

A standard enum is usually implemented as an int32, the compiler will handle your enum as a synonym of int32 . Once a list of values is created for a enumeration those values are stored as literals against their display name(access name given at the time of declaration of enum).

Where should enums be defined Java?

You can have a public CARD_SUITE enum in your Card class. It makes sense if the enum is just a list of values. If your enum has some methods, implements some interfaces, etc. it is better to define it in its own file.

Where are enums used?

You should use enum types any time you need to represent a fixed set of constants. That includes natural enum types such as the planets in our solar system and data sets where you know all possible values at compile time—for example, the choices on a menu, command line flags, and so on.


1 Answers

Why treat enums differently to other types? Keep them in the same namespace as they're likely to be used - and assuming they're going to be used by other classes, make them top-level types in their own files.

The only type of type which I do commonly clump together is delegates - I sometimes have a Delegates.cs file with a bunch of delegates in. Less so with .NET 3.5 and Func/Action, mind you.

like image 158
Jon Skeet Avatar answered Sep 22 '22 03:09

Jon Skeet