tags:

views:

850

answers:

13

I have a class which uses an enumeration, the enum is currently in it's 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 it's own cs file?

Edit

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

+8  A: 

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
+7  A: 

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
+17  A: 

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
It adds noise to the directory when browsing, that's what I meant by wasteful.
Finglas
@Finglas - one person's noise is another person's signal!
Jeff Sternal
A: 

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.

sadboy
+5  A: 

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
A: 

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 }
nichromium
+2  A: 

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
+4  A: 

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
+2  A: 

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
A: 

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
+1  A: 

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
A: 

hi ,

Enum are very useful in the realtime projects if want login application in ur project we can use public enum MemeberReturn { Successfull, Invalid, Failure, InvalidloginIdorpassword, }

u can use this method in other class..

Can't find any link to the question.
Simpzon
How does this relate to the question?
Callum Rogers
A: 

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