Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should enumerations be placed in a separate file or within another class?

Tags:

c#

.net

enums

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,
    }
}
like image 438
Icono123 Avatar asked Mar 17 '10 16:03

Icono123


People also ask

Where should enum be placed?

Put the enums in the namespace where they most logically belong. (And if it's appropriate, yes, nest them in a class.)

Can you define an enum within a class?

Yes, we can define an enumeration inside a class. You can retrieve the values in an enumeration using the values() method.

What are the advantages of using enumerations?

The benefits of using enumerations include: Reduces errors caused by transposing or mistyping numbers. Makes it easy to change values in the future. Makes code easier to read, which means it is less likely that errors will creep into it.

Are enumerations classes?

Yes enum are type of Java Class. The values of an enum are the only possible instances of this class.


2 Answers

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.

like image 185
Jon Skeet Avatar answered Oct 02 '22 17:10

Jon Skeet


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

like image 25
heisenberg Avatar answered Oct 02 '22 17:10

heisenberg