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,
}
}
Put the enums in the namespace where they most logically belong. (And if it's appropriate, yes, nest them in a class.)
Yes, we can define an enumeration inside a class. You can retrieve the values in an enumeration using the values() method.
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.
Yes enum are type of Java Class. The values of an enum are the only possible instances of this class.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With