Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do some people use "None" as enumeration member?

Tags:

c#

I noticed that some enumerations have "None" as a enumeration member.

For example what I mean

enum Mode
{
    Mode1 = 1,
    Mode2 = 2,
    Mode3 = 3,
    None = 4
}

Why do they use it ? In what cases solution with a none member is more preferable (less preferable) ?

like image 409
Александр Д. Avatar asked Apr 16 '10 06:04

Александр Д.


People also ask

What is the point of enumeration?

Enumerations make for clearer and more readable code, particularly when meaningful names are used. The benefits of using enumerations include: Reduces errors caused by transposing or mistyping numbers. Makes it easy to change values in the future.

Are enums zero based?

The default value of an uninitialized enumeration, just like other value types, is zero.

Are enums 0 based C#?

Enum ValuesThe first member of an enum will be 0, and the value of each successive enum member is increased by 1. You can assign different values to enum member. A change in the default value of an enum member will automatically assign incremental values to the other members sequentially.

What is default for enum C#?

The default value for an enum is zero.


1 Answers

None is important for [Flags] enums, and should have the value 0. Otherwise... questionable. A Nullable<Mode> would also suffice. But None may be demanded for their serialization or ORM layers (it might map to an expected string / int-value etc). Or it might just make the API simpler.

like image 99
Marc Gravell Avatar answered Sep 18 '22 10:09

Marc Gravell