tags:

views:

166

answers:

3

I currently have a class file with the following enumeration:

using System;

namespace Helper
{
    public enum ProcessType
    {
        Word = 0,
        Adobe = 1,
    }
}

Or should I include the enumeration in the class where it's being used?

I noticed Microsoft creates a new class file for DockStyle:

using System;
using System.ComponentModel;
using System.Drawing.Design;

namespace System.Windows.Forms
{
    public enum DockStyle
    {
        None = 0, 
        Top = 1,
        Bottom = 2,
        Left = 3,
        Right = 4,.
        Fill = 5,
    }
}
+12  A: 

If the enum is only relevant to one class, it may make sense to make it a nested type. If it could be used elsewhere, it makes sense to make it a top-level type.

Jon Skeet
+1 Required to up-vote because you are Jon Skeet
Josh Stodola
I hesitated up-voting, because Jon Skeet has more reputation than God, but it's a good answer nonetheless.
Critical Failure
@Critical Failure: If it's any consolation, it's way too late in the day for an upvote to make any rep difference.
Jon Skeet
On the other hand, Microsoft recommends to avoid public nested types, per FxCop rule (CA1034: NestedTypesShouldNotBeVisible): http://msdn.microsoft.com/en-us/library/ms182162.aspx
binarycoder
He could still keep the enumeration type in the file but at the top level. Keeping it in the file is still good if the only time it would ever change is when it's associated class would change and vice-versa.
galford13x
@binarycoder: The reasons given aren't particularly compelling to me. FxCop would hate the builder that is generated for each Protocol Buffer class... ah well. I still think they're useful. Sometimes you *do* want logical grouping at a finer level than "namespace".
Jon Skeet
+5  A: 

Typically I see enumera*tions* placed in the class where they are being used if no other class will be using them, otherwise in their own file.

kekekela
A: 

I tend to create an Enum.cs file with all of my general use Enums, if I have enums that pretain to only ONE class then I have it nested.

xximjasonxx