44

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?

aceinthehole
  • 5,122
  • 11
  • 38
  • 54
  • 2
    Anything "best-practice" related will always be subjective, but I think your coworker is wrong. The .NET framework doesn't include an Enums namespace, and for good reason. Put the enums in the namespace where they most logically belong. (And if it's appropriate, yes, nest them in a class.) – John Rudy Oct 17 '08 at 20:28
  • 7
    Until your collegue explained her/his problem, "stupid" is not really a metric you can act upon. – DK. Oct 17 '08 at 21:34
  • Stated like this, the co-worker is wrong. If the solution has many projects and the enums are used in multiple projects, it makes sense, to have them in one place. – Bakudan Sep 02 '16 at 11:10

10 Answers10

49

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
9

Also, namespaces are for separation of things that belong together logically. Not all classes belong in the same namespace just because they are classes. Likewise, not all enums belong in the same namespace just because they are enums. Put them with the code they logically belong in.

Nick
  • 9,113
  • 4
  • 25
  • 29
5

I generally try to put all my different types (classes, interfaces and enums) in their own files, regardless of how small they are. It just makes it much easier to find and manage the file they're in, especially if you don't happen to be in Visual Studio and have the "go to definition" feature available. I've found that nearly every time I've put a "simple" type like that in another class, I end up either adding on to it later on, or reusing it in a way that it no longer makes sense for it to not have its own file.

As far as which namespace, it really depends on the design of whatever you're developing. In general, I try to mimic the .NET framework's convention.

Daniel Schaffer
  • 56,753
  • 31
  • 116
  • 165
5

I try to put everything associated with a class in the class. That includes not just enums, but also constants. I don't want to go searching elsewhere for the file or class containing the enums. In a large app with lots of classes and folders, it wouldn't always be obvious where to put the enum file so it would be easy to find.

If the enum if used in several closely-related classes, you could create a base class so that the common types like enums are shared there.

Of course, if an enum is really generic and widely used, you may want to create a separate class for them, along with other generic utilities.

DOK
  • 32,337
  • 7
  • 60
  • 92
5

I think you put Enums and Constants in the class that consumes them or that uses them to control code decisions the most and you use code completion to find them. That way you don't have to remember where they are, they are associated with the class. So for example if I have a ColoredBox class then I don't have to think about where they are at. They would be part of ColoredBox. ColoredBox.Colors.Red, ColoredBox.Colors.Blue etc. I I think of the enum and constant as a property or description of that class. If it used by multiple classes and no one class reigns supreme then it is appropriate to have an enum class or constants class. This follows rules of encapsulation. Isolating properties from dissimilar classes. What if you decide to change the RGB of Red in Cirle objects but you don't want to change the red for ColoredBox objects? Encapsulating their properties enables this.

Al C
  • 51
  • 1
  • 1
3

I use nested namespaces for this. I like them better than putting the enum within a class because outside of the class you have to use the full MyClass::MyEnum usage even if MyEnum is not going to clash with anything else in scope.

By using a nested namespace you can use the "using" syntax. Also I will put enums that relate to a given subsystem in their own file so you don't get dependency problems of having to include the world to use them.

So in the enum header file you get:

// MyEnumHeader.h
// Consolidated enum header file for this dll,lib,subsystem whatever.
namespace MyApp
{
  namespace MyEnums
  {
    enum SomeEnum { EnumVal0, EnumVal1, EnumVal2 };
  };
};

And then in the class header file you get:

// MyInterfaceHeader.h
// Class interfaces for the subsystem with all the expected dependencies.

#include "MyEnumHeader.h"

namespace MyApp
{
  class MyInterface
  {
  public:
    virtual void DoSomethingWithEnumParam (MyEnums::SomeEnum enumParam) = 0;
  };
};

Or use as many enum header files as makes sense. I like to keep them separate from the class headers so the enums can be params elsewhere in the system without needing the class headers. Then if you want to use them elsewhere you don't have to have the encapsulating class defs as you would if the enums were declared within the classes.

And as mentioned before, in the outer code you can use the following:

using namespace MyApp::MyEnums;
charlest
  • 31
  • 4
2

What environment?

In .NET I usually create an empty class file, rename it to MyEnum or whatever to indicate it holds my enum and just declare it in there.

Nick
  • 9,113
  • 4
  • 25
  • 29
2

If my enumeration has any chance of ever being used outside the class I intend to use it, I create a separate source file for the enum. Otherwise I will place it inside the class I intend to use it.

John Kraft
  • 6,811
  • 4
  • 37
  • 53
2

Usually I find that the enum is centered around a single class -- as a MyClassOptions type of thing.

In that case, I place the enum in the same file as MyClass, but inside the namespace but outside the class.

namespace mynamespace
{
  public partial class MyClass
  {
  }
  enum MyClassOptions
  {
  }
}
Chris Cudmore
  • 29,793
  • 12
  • 57
  • 94
1

I tend to define them, where their use is evident in the evident. If I have a typedef for a struct that makes use of it for some reason...

typedef enum {
  HI,
  GOODBYE
} msg_type;

typdef struct {
 msg_type type;
 union {
   int hivar;
   float goodbyevar;
  }
} msg;
Nicholas Mancuso
  • 11,599
  • 6
  • 45
  • 47