197

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.

4
  • 97
    If you used magic numbers, you wouldn't have this problem at all. Feb 17, 2010 at 17:42
  • 7
    Should this be community wiki? There's no correct answer and no real technical considerations apart from IDE capabilities. Feb 17, 2010 at 18:04
  • 1
    They can still be in the same namespace even if they're in different files. If you're asking the secondary question of whether to create an .Enums namespace AND a new file, then I would say, usually, no. But otherwise, you might have your understanding of namespaces wrong and should read about them - (not much to them, just an organization mechanism) Feb 17, 2010 at 18:48
  • 1
    Declaring enum in it's own file allow programmer to locate enum easily using command window (>of [enum Name])
    – Riju
    Feb 10, 2015 at 6:30

15 Answers 15

122

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.

5
  • 9
    It adds noise to the directory when browsing, that's what I meant by wasteful.
    – Finglas
    Feb 17, 2010 at 17:41
  • 146
    @Finglas - one person's noise is another person's signal! Feb 17, 2010 at 17:46
  • 7
    Usually there's one class that's most closely associated. But if that changes, if someone comes along at a time decides to take a dependency on the enum, then it's time for a refactor. Sep 18, 2015 at 16:06
  • 3
    If the enum is to be shared across different classes it's preferable to put it in a separate file.
    – Konrad
    Oct 15, 2018 at 12:42
  • do we declare the enum in the namespace or class level?
    – paraJdox1
    Jun 4, 2021 at 12:52
87

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.

2
  • 13
    "Putting a single type in each file also makes it easier to identify changes in source control systems without diffing." A fear of diffing should not form the foundation of your design decisions. I'd even argue that anyone who doesn't know how to properly diff a file in source control isn't really using source control at all. Feb 24, 2015 at 21:45
  • 3
    Nah. I don't see fear here. It's always nice to have conveniency and readability, which comes with increased work efficiency. Having a well organized codebase lessens the time spent on diffs and conflicts. @DanBechard Dec 3, 2020 at 18:22
66

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.

7
  • 4
    I think this is probably the best option as it: 1) is only one file instead of many which could be considered as cluttering the directory 2) is clear what is contained within the file 3) means that you know where to find an enum instead of it being in a file containing a class which is related but not necessarily the only class using it
    – dav_i
    Sep 14, 2012 at 9:51
  • 7
    I absolutely do not like this. As said in the answer of James Curran, enums have a relation to classes mostly. When putting them all in one global file, they're not even in a directory (for a sub-namespace) anymore where they could thematically belong to.
    – Ray
    Aug 23, 2014 at 11:26
  • 3
    Yes @DebugErr, I agree with you. Since this answer was posted back in 2010 I have changed between various approaches and tend to go with one file per type, or declaring enums in the same file as the related class. Aug 23, 2014 at 16:28
  • 2
    @Ray ...enums have a relation to classes mostly.. This is where you lost me. Please give an example of how you would handle enums that have relations to several classes?
    – basickarl
    Nov 20, 2019 at 14:25
  • @KarlMorrison Please, that comment is 5 years old. Anyway, I added the word "mostly" for a reason. Enums have a relation to more than just classes, like namespaces too. If I had an AnchorStyle enum used throughout a UI library, I'd typically also have a UI sub namespace and corresponding folder. I'd then place it in an AnchorStyle.cs file in the UI folder where I can easily find it, not in a generically named "Enums.cs" file.
    – Ray
    Nov 20, 2019 at 16:07
49

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.

10
  • What about if you want to reuse enums in a different solution of the same enterprise project? Binding enums with class using it would be very difficulty to reuse it.
    – mko
    Oct 9, 2019 at 9:35
  • @mko: The project reference already means both the class and enum will be available to the different solution. What would make it difficult? Oct 9, 2019 at 16:00
  • Sure, but do you really want to share entire class with its logic if you only want to use enums. Further more what if a different classes share same enumeration. Where would you place it?
    – mko
    Oct 9, 2019 at 18:32
  • @mko: With a project reference, you'll get both types whether they are in different files or not. I'm having trouble figuring out what you're asking. Oct 9, 2019 at 19:17
  • Well I am not talking about project reference, you are. I am talking about moving enums to a shared project file and be able to reuse it in multiple project without exposing entire classes. You say "There is no compelling reason to put two public types in the same file simply because one is an enumeration". Maybe there is a reason to put all enums in the same file if you follow my explanation.
    – mko
    Oct 10, 2019 at 9:41
30

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.

25

I place mostly inside in namespace and outside of class so that it is easily accessible other classes in that namespace like below.

namespace UserManagement
{
    public enum UserStatus { Active, InActive }
    class User
    {
        ...
    }
}
1
  • Wow. I havn't known enums can be placed into the namespace directly. I'm going with this answer. Inside my MVC struct they will be placed inside the controller what makes logic to me. Thanks for this. Upvoted.
    – C4d
    Aug 15, 2016 at 11:48
12

Generally I prefer my enums to be in the same file as the Class that it will most probably be an attribute of. If for example I have a class Task then the enum TaskStatus will be in the same file.

However, if I have enums of a more generic nature, then I keep them contextually in various files.

2
  • What if different class also uses same enum?
    – mko
    Oct 9, 2019 at 9:36
  • 3
    @mko - That's why I said (back in 2010 when I answered this) that if enums have a more generic nature I keep them in separate files. By contextually I meant that in some cases some enums might be in a separate file, and in other cases, I might group a set of enum declarations in a single file. Oct 10, 2019 at 10:10
12

It depends on what access is needed.

If the enum is only used by a single class, it's okay to declare it within that class because you don't need to use it anywhere else.

For enums used by multiple classes or in a public API, then I will always keep the definition in its own file in the appropriate namespace. It's far easier to find that way, and the strategy follows the pattern of one-object-per-file, which is good to use with classes and interfaces as well.

8

I think that depends on the scope of the enum. For example if the enum is specific to one class, for example used to avoid the magic constant scenario, then I would say put it in the same file as the class:

enum SearchType { Forward, Reverse }

If the enum is general and can be used by several classes for different scenarios, then I would be inclined to use put it in its own file. For example the below could be used for several purposes:

enum Result { Success, Error }
7

I tend to put enums in their own file for a very simple reason: as with classes and structs, it's nice to know exactly where to look if you want to find a type's definition: in the file of the same name. (To be fair, in VS you can always use "Go to Definition," too.)

Obviously, it can get out of hand. A colleague where I work even makes separate files for delegates.

6

One advantage of using a separate file for enums is that you can delete the original class that used the enum and write a new class using the enum.

If the enum is independent of the original class then putting it in a separate file makes future changes easier.

6

If you are using the USysWare File Browser add-in for Visual Studio, you can very quickly find files of particular names in your solution. Imagine looking for an enum that is not in its own file but instead buried in some file in a gigantic solution.

For small solutions, it doesn't matter, but for large ones, it becomes all the more important to keep classes and enums in their own files. You can quickly find them, edit them, and more. I highly, highly recommend putting your enum in its own file.

And as was stated... How wasteful is a file that ends up only being a couple of kb anyways?

1
  • I use that add-in as well, it's quite handy. I'd put enums in their own file, no matter if the solution is big or small. May 21, 2013 at 13:57
6

Very simple huge advantage to separate file. When any object is in its own MyObjectName.cs file... you can go to solution explorer and type MyObjectName.cs and be shown exactly 1 file. Anything that makes debugging better is nice.

Another advantage on a similar note, if you search all files (ctrl+shft+F) for a name, you may find 20 references to the name in the same file... and that found name will be part of different objects. In the Find Results window all you can see is the line number and the file name. You would have to open the file and scroll to figure out which object the found reference was in.

Anything that makes debugging easier, I like.

4

If you have multiple projects in one solution. Then better create another project Utilities. Then create a Folder \Enumerations and create a nested static class. And then assign each static class where you will create enum that corresponds to the name of your projects. For example you have a project named DatabaseReader and DatabaseUsers then you may name the static class like

public static class EnumUtility {
    #region --Database Readers Enum
    public static class EnumDBReader {
         public enum Actions { Create, Retrieve, Update, Delete}; 
    }
    #endregion

    #region --Database Users Enum
    public static class EnumDBUsers {
         public enum UserIdentity { user, admin }; 
    }
    #endregion

}

Then entire enum that can be used in the entire solutions per projects will be declared on it. Use #region to separate each concern. By this, it is easier to look for any enums

1

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.