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.

Finglas
  • 15,518
  • 10
  • 56
  • 89
  • 98
    If you used magic numbers, you wouldn't have this problem at all. – MusiGenesis Feb 17 '10 at 17:42
  • 7
    Should this be community wiki? There's no correct answer and no real technical considerations apart from IDE capabilities. – Jeff Sternal Feb 17 '10 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) – Jason Kleban Feb 17 '10 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 '15 at 06:30

15 Answers15

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.

James Curran
  • 101,701
  • 37
  • 181
  • 258
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.

Jeff Sternal
  • 47,787
  • 8
  • 93
  • 120
  • 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. – Dan Bechard Feb 24 '15 at 21:45
  • 4
    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 – DarkWingDuck Dec 03 '20 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.

Fredrik Mörk
  • 155,851
  • 29
  • 291
  • 343
  • 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 '12 at 09: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 '14 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. – Fredrik Mörk Aug 23 '14 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 '19 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 '19 at 16:07
  • Yes, revisiting this question nearly 10 years later I can say that it was a long time since I hade an `Enums.cs` file in a project. I typically put each type in their own file, placed in the structure where they belong. – Fredrik Mörk Nov 20 '19 at 20:38
  • @FredrikMörk Thanks for the update. Nice to know you've changed to a better standard! – basickarl Nov 21 '19 at 09:24
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.

Bryan Watts
  • 44,911
  • 16
  • 83
  • 88
  • 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 09 '19 at 09:35
  • @mko: The project reference already means both the class and enum will be available to the different solution. What would make it difficult? – Bryan Watts Oct 09 '19 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 09 '19 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. – Bryan Watts Oct 09 '19 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 '19 at 09:41
  • @mko: I do. Your scenario was far enough from .NET idioms that I didn't even consider it. I'll give you points for finding an edge case, but it's an outlier on the bell curve that doesn't change my general advice. – Bryan Watts Oct 11 '19 at 02:35
  • Well, it is a real world scenario, and it is impossible to solve it differently without pointless mapping between same enum items of a different enum type... Imagine if you add one new enum item and try to find all the references and add that item. To me it is a horror story – mko Oct 12 '19 at 08:42
  • @mko: The scenario of referencing individual source files from a different project is weird. I've been doing .NET since 1.0 and have never seen anyone do that. If your development scenario is this far out of the norm, you can safely ignore the advice in my answer. – Bryan Watts Oct 13 '19 at 18:37
  • @mko I have being doing .net since 1.0. Does it mean we are buddies? :) Shared projects are introduces not to to long ago, so I am not sure what is the purpose of a share project if you call it weird? So Microsoft came up with a shared project thing that according to you should not be shared across same enterprise project. Hm, ok. – mko Oct 14 '19 at 07:28
  • @mko: I find lots of things weird. This is not equal to saying they aren't useful. You are free to ignore my answer if it does not suit your taste. – Bryan Watts Oct 14 '19 at 22:03
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.

Tommy Carlier
  • 7,951
  • 3
  • 26
  • 43
26

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
    {
        ...
    }
}
Adeel
  • 19,075
  • 4
  • 46
  • 60
  • 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 '16 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.

Nikos Steiakakis
  • 5,605
  • 7
  • 44
  • 54
  • What if different class also uses same enum? – mko Oct 09 '19 at 09: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. – Nikos Steiakakis Oct 10 '19 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.

Jon Seigel
  • 12,251
  • 8
  • 58
  • 92
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 }
Vishal Mistry
  • 376
  • 1
  • 5
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.

Dan Tao
  • 125,917
  • 54
  • 300
  • 447
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.

Doug Ferguson
  • 2,538
  • 2
  • 16
  • 23
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?

Batalla
  • 61
  • 1
  • 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. – Rui Jarimba May 21 '13 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.

Developer Guy
  • 2,318
  • 6
  • 19
  • 37
vbp13
  • 1,040
  • 1
  • 10
  • 20
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

Israel Ocbina
  • 517
  • 8
  • 14
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.

Patrick Kafka
  • 9,795
  • 3
  • 29
  • 44