Reputation: 16747
I often see Simple (non-Flags) Enums declared like this:
enum Something
{
Thingy = 1,
Thangamijig = 2,
Whatsit = 3
}
Why include the optional numeric values? Is this considered a best practice?
The MSDN guidelines for enum design don't seem to explain a reason for doing this.
A potential answer just ocurred to me: is it to ensure previously serialized objects containing instances of the enum won't become invalid or have their meaning changed if the enum were to be modified (say by reordering the values or inserting a new value)?
That seems plausible. Are there other reasons?
Upvotes: 3
Views: 412
Reputation: 62544
It is important when you are store enum values somewhere, this could be data base, configuration files, etc...
Imagine that initally you've an enum like this
enum TransactionType
{
Debt,
Deposit
}
And stored TransactionType for ClientA as TransactionType.Deposit
(int value 1) in a database.
Later, an other developer had added new type so enum now looks like
enum TransactionType
{
NotAssigned,
Debt,
Deposit
}
So when application read transaction type for ClientA from a database (int == 1) it becomes as TransactionTYpe.Debt
.
ClientA will be confused because instead of Deposit it will see in online system that he has Debt.
To avoid such issues you should add initial values.
Upvotes: 1
Reputation: 89735
Like you said first and most important reason is to make sure that when you changed order of the enum, you did not break anything.
By default enums starts with 0, so if you have variables that should have some values you should have numeric value. Sometimes you will see that people using first item as None, because it has 0 value.
Another reason for using number is if you are using Flags then you don't have option, you have to use values
[Flags]
enum Days2
{
None = 0x0,
Sunday = 0x1,
Monday = 0x2,
Tuesday = 0x4,
Wednesday = 0x8,
Thursday = 0x10,
Friday = 0x20,
Saturday = 0x40
}
class MyClass
{
Days2 meetingDays = Days2.Tuesday | Days2.Thursday;
}
Upvotes: 3
Reputation: 63552
This is very important if you're going to save these values externally, like a database, and want to map their value back to code.
If not assigned explicitly, they will be set sequentially at compile time.
For instance:
enum Something
{
Thingy,
Thangamijig,
Whatsit
}
So Thingy == 0
, Thanamijig == 1
, Whatsit == 2
This enum is used and saved to the database for a period of time and then some other developer finds a new use for Something
and changes it... poorly:
enum Something
{
Whosits,
Thingy,
Thangamijig,
Whatsit
}
Now Thingy == 1
, Thanamijig == 2
, Whatsit == 3
and you no longer have the proper mapping.
This is just one case where not explicitly defining what each enum value maps to can cause a large problem.
The default underlying type of the enumeration elements is int. By default, the first enumerator has the value 0, and the value of each successive enumerator is increased by 1. For example:
enum Days {Sat, Sun, Mon, Tue, Wed, Thu, Fri};
In this enumeration, Sat is 0, Sun is 1, Mon is 2, and so forth. Enumerators can have initializers to override the default values. For example:
enum Days {Sat=1, Sun, Mon, Tue, Wed, Thu, Fri};
In this enumeration, the sequence of elements is forced to start from 1 instead of 0.
Upvotes: 7
Reputation: 49261
Think of the a larger database.
Lets say you have an array of constant strings, with like 250 strings.
But in some part of your code, you don't really want to use all those strings, but for example just the last 3 of them.
This feature allows you to do that:
enum LastThree
{
Thingy = 247,
Thangamijig = 248,
Whatsit = 249
}
Upvotes: 1
Reputation: 3077
Often when interfacing with other code. Sometimes hardware that has pre-defined values for the enums.
Sometimes people use them as flags, values 0,1,2,4... then they can be OR'd together to an int. The int can later be tested against the enums.
Upvotes: 1
Reputation: 150158
Being able to assign a specific value is useful for interoperability with another system that does not have access to the types defined in C#.
For example, a COM program or external web service may define certain numerical values for a property that you choose to represent as an enum in C#.
Upvotes: 4