Should an Enum start with a 0 or a 1?

C#.NetEnums

C# Problem Overview


Imagine I have defined the following Enum:

public enum Status : byte
{
    Inactive = 1,
    Active = 2,
}

What's the best practice to use enum? Should it start with 1 like the above example or start with 0 (without the explicit values) like this:

public enum Status : byte
{
    Inactive,
    Active
}

C# Solutions


Solution 1 - C#

Framework Design Guidelines:

> ✔️ DO provide a value of zero on simple enums. > > Consider calling the value something like "None." If such a value is not appropriate for this particular enum, the most common default value for the enum should be assigned the underlying value of zero.

Framework Design Guidelines / Designing Flag Enums:

> ❌ AVOID using flag enum values of zero unless the value represents "all flags are cleared" and is named appropriately, as prescribed by the next guideline. > > ✔️ DO name the zero value of flag enums None. For a flag enum, the value must always mean "all flags are cleared."

Solution 2 - C#

Well, I guess I stand in disagreement with most answers that say not to explicitly number them. I always explicitly number them, but that is because in most cases I end up persisting them in a data stream where they are stored as an integer value. If you don't explicitly add the values and then add a new value you can break the serialization and then not be able to accurately load old persisted objects. If you are going to do any type of persistent store of these values then I would highly recommend explicitly setting the values.

Solution 3 - C#

An Enum is a value type and its default value (for example for an Enum field in a class) will be 0 if not initialized explicitly.

Therefore you generally want to have 0 as an defined constant (e.g. Unknown).

In your example, if you want Inactive to be the default, then it should have the value zero. Otherwise you might want to consider adding a constant Unknown.

Some people have recommended that you don't explicitly specify values for your constants. Probably good advice in most cases, but there are some cases when you will want to do so:

  • Flags enums

  • Enums whose values are used in interop with external systems (e.g. COM).

Solution 4 - C#

Unless you have a specific reason to change it, leave enums with their default values, which begin at zero.

public enum Status : byte
{
    Inactive,
    Active
}

Solution 5 - C#

Unless you have a good reason to use the raw values, you should only ever be using implicit values and referencing them with Status.Active and Status.Inactive.

The catch is that you might want to store data in a flat file or DB, or use a flat file or DB that someone else created. If you're making it yourself, make it so the numbering fits what the Enum is used for.

If the data is not yours, of course you're going to want to use whatever the original dev had used as a numbering scheme.

If you're planning on using the Enum as a set of flags, there is a simple convention that's worth following:

enum Example
{
  None      = 0,            //  0
  Alpha     = 1 << 0,       //  1
  Beta      = 1 << 1,       //  2
  Gamma     = 1 << 2,       //  4
  Delta     = 1 << 3,       //  8
  Epsilon   = 1 << 4,       // 16
  All       = ~0,           // -1
  AlphaBeta = Alpha | Beta, //  3
}

Values should be powers of two and can be expressed using bit-shift operations. None, obviously should be 0, but All is less obviously -1. ~0 is the binary negation of 0 and results in a number that has every bit set to 1, which represents a value of -1. For compound flags (often used for convenience) other values may be merged using the bitwise or operator |.

Solution 6 - C#

I would say, it depends on how you use them. For flagging enum it is a good practice to have 0 for None value, like that:

[Flags]
enum MyEnum
{
    None = 0,
    Option1 = 1,
    Option2 = 2,
    Option3 = 4,
    All = Option1 | Option2 | Option3,
}

When your enum is likely to be mapped to a database lookup table, I'd start it with 1. It should not matter much for professionally written code, but this improves readability.

In other cases I'd leave it as it is, giving no care whether they start with 0 or 1.

Solution 7 - C#

I'd say best practice is to not number them and let it be implicit - which would start from 0. Since its implicit its the language preference which is always good to follow :)

Solution 8 - C#

I would start a boolean type enum with a 0.

Unless "Inative" means something other than "Inactive" :)

This retains the standard for those.

Solution 9 - C#

If not specified numbering starts at 0.

It is important to be explicit since enums are often serialized and stored as an int, not a string.

For any enum stored in the database, we always explicitly number the options to prevent shifting and reassignment during maintenance.

According to Microsoft, the recommended convention is use the first zero option to represent an uninitialized or the most common default value.

Below is a shortcut to start numbering at 1 instead of 0.

public enum Status : byte
{
    Inactive = 1,
    Active
}

If you wish to set flag values in order to use bit operators on enum values, don't start numbering at the zero value.

Solution 10 - C#

Don't assign any numbers. Just use it like it supposed to be used.

Solution 11 - C#

If you start at 1, then you can easily get a count of your things.

{
    BOX_THING1     = 1,
    BOX_THING2     = 2,
    BOX_NUM_THING  = BOX_THING2
};

If you start at 0, then use the first one as a value for uninitialized things.

{
    BOX_NO_THING   = 0,
    BOX_THING1     = 1,
    BOX_THING2     = 2,
    BOX_NUM_THING  = BOX_THING2
};

Solution 12 - C#

First of all, unless you're specifying specific values for a reason (the numeric value has meaning somewhere else, i.e. The Database or external service) then don't specify numeric values at all and let them be explicit.

Second of all, you should always have a zero value item (in non-flags enums). That element will be used as the default value.

Solution 13 - C#

Don't start them at 0 unless there's a reason to, such as using them as indices to an array or list, or if there's some other practical reason (like using them in bitwise operations).

Your enum should start exactly where it needs to. It needn't be sequential, either. The values, if they are explicitly set, need to reflect some semantic meaning or practical consideration. For example, an enum of "bottles on the wall" should be numbered from 1 to 99, while an enum for powers of 4 should probably start at 4 and continue with 16, 64, 256, etc.

Furthermore, adding a zero-valued element to the enum should only be done if it represents a valid state. Sometimes "none," "unknown," "missing," etc. are valid values, but many times they are not.

Solution 14 - C#

if the enum starts with zero then no need to assign integer values.It starts with 0 & increment by 1. Inactive = 0, Active = 1.

 public enum Status : byte{
   Inactive,
   Active
}

If you want to assign specific value for 1st one, you need to assign the value for it.Here, Inactive = 1, Active = 0.

 public enum Status : byte{
    Inactive =1,
    Active =0
 }

Solution 15 - C#

I like to start my enums at 0, since that's the default, but I also like to include a Unknown value, with a value of -1. This then becomes the default and can help with debugging sometimes.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAcaz SouzaView Question on Stackoverflow
Solution 1 - C#Andrey TaptunovView Answer on Stackoverflow
Solution 2 - C#pstrjdsView Answer on Stackoverflow
Solution 3 - C#JoeView Answer on Stackoverflow
Solution 4 - C#FishBasketGordoView Answer on Stackoverflow
Solution 5 - C#zzzzBovView Answer on Stackoverflow
Solution 6 - C#Michael SagalovichView Answer on Stackoverflow
Solution 7 - C#John HumphreysView Answer on Stackoverflow
Solution 8 - C#Mark SchultheissView Answer on Stackoverflow
Solution 9 - C#druView Answer on Stackoverflow
Solution 10 - C#HoochView Answer on Stackoverflow
Solution 11 - C#Jonathan Cline IEEEView Answer on Stackoverflow
Solution 12 - C#Justin NiessnerView Answer on Stackoverflow
Solution 13 - C#wprlView Answer on Stackoverflow
Solution 14 - C#Madusha RavishaniView Answer on Stackoverflow
Solution 15 - C#Tomas McGuinnessView Answer on Stackoverflow