Questions tagged [enums]

A data type consisting of a set of named values called elements, members or enumerators of the type.

This tag is for questions related to enumeration, enumerated-types (or enums) as related to programming.

In computer programming, an enumerated type (also called enumeration or enum) is a data type consisting of a set of named values called elements, members or enumerators of the type. The enumerator names are usually identifiers that behave as constants in the language. A variable that has been declared as having an enumerated type can be assigned any of the enumerators as a value.

From the wikipedia article on enumerated type

Rust and Swift

Unlike the other languages that take inspiration from , the enums in and are sum types (see, for example, sum types in Haskell):

19721 questions
683
votes
14 answers

How do I convert an enum to a list in C#?

Is there a way to convert an enum to a list that contains all the enum's options?
newWeb
597
votes
10 answers

What is an idiomatic way of representing enums in Go?

I'm trying to represent a simplified chromosome, which consists of N bases, each of which can only be one of {A, C, T, G}. I'd like to formalize the constraints with an enum, but I'm wondering what the most idiomatic way of emulating an enum is in…
carbocation
  • 7,064
  • 7
  • 21
  • 24
568
votes
15 answers

Can enums be subclassed to add new elements?

I want to take an existing enum and add more elements to it as follows: enum A {a,b,c} enum B extends A {d} /*B is {a,b,c,d}*/ Is this possible in Java?
Mike
  • 54,052
  • 71
  • 166
  • 213
560
votes
42 answers

How to enumerate an enum with String type?

enum Suit: String { case spades = "♠" case hearts = "♥" case diamonds = "♦" case clubs = "♣" } For example, how can I do something like: for suit in Suit { // do something with suit print(suit.rawValue) } Resulting…
Lucien
  • 7,795
  • 4
  • 27
  • 30
528
votes
27 answers

What are enums and why are they useful?

Today I was browsing through some questions on this site and I found a mention of an enum being used in singleton pattern about purported thread safety benefits to such solution. I have never used enums and I have been programing in Java for more…
MatBanik
  • 24,206
  • 38
  • 107
  • 172
524
votes
9 answers

Why is enum class preferred over plain enum?

I heard a few people recommending to use enum classes in C++ because of their type safety. But what does that really mean?
Oleksiy
  • 30,852
  • 19
  • 65
  • 114
522
votes
24 answers

Getting attributes of Enum's value

I would like to know if it is possible to get attributes of the enum values and not of the enum itself? For example, suppose I have the following enum: using System.ComponentModel; // for DescriptionAttribute enum FunkyAttributesEnum { …
Alex K
  • 9,835
  • 7
  • 23
  • 34
467
votes
13 answers

Enum String Name from Value

I have an enum construct like this: public enum EnumDisplayStatus { None = 1, Visible = 2, Hidden = 3, MarkedForDeletion = 4 } In my database, the enumerations are referenced by value. My question is, how can I turn the number…
jdee
  • 10,282
  • 10
  • 36
  • 35
428
votes
10 answers

How to bind RadioButtons to an enum?

I've got an enum like this: public enum MyLovelyEnum { FirstSelection, TheOtherSelection, YetAnotherOne }; I got a property in my DataContext: public MyLovelyEnum VeryLovelyEnum { get; set; } And I got three RadioButtons in my WPF…
Sam
  • 26,538
  • 45
  • 157
  • 240
428
votes
30 answers

enum to string in modern C++11 / C++14 / C++17 and future C++20

Contrary to all other similar questions, this question is about using the new C++ features. 2008 c Is there a simple way to convert C++ enum to string? 2008 c Easy way to use variables of enum types as string in C? 2008 c++ How to easily map c++…
oHo
  • 41,098
  • 25
  • 141
  • 183
427
votes
34 answers

How to get names of enum entries?

I would like to iterate a TypeScript an enum type and get each enumerated symbol name, e.g.: enum myEnum { entry1, entry2 } for (var entry in myEnum) { // use entry's name here, e.g., "entry1" }
CalvinDale
  • 6,399
  • 5
  • 23
  • 35
423
votes
19 answers

Using Enum values as String literals

What is the best way to use the values stored in an Enum as String literals? For example: public enum Modes { some-really-long-string, mode1, mode2, mode3 } Then later I could use Mode.mode1 to return its string representation as…
Larry
  • 9,699
  • 12
  • 55
  • 81
422
votes
15 answers

Enum "Inheritance"

I have an enum in a low level namespace. I'd like to provide a class or enum in a mid level namespace that "inherits" the low level enum. namespace low { public enum base { x, y, z } } namespace mid { public enum consume :…
CodeMonkey1313
  • 14,477
  • 17
  • 71
  • 109
422
votes
5 answers

How to get C# Enum description from value?

I have an enum with Description attributes like this: public enum MyEnum { Name1 = 1, [Description("Here is another")] HereIsAnother = 2, [Description("Last one")] LastOne = 3 } I found this bit of code for retrieving the…
davekaro
  • 5,583
  • 2
  • 26
  • 22
383
votes
8 answers

Best way to create enum of strings?

What is the best way to have a enum type represent a set of strings? I tried this: enum Strings{ STRING_ONE("ONE"), STRING_TWO("TWO") } How can I then use them as Strings?
Dori
  • 17,514
  • 16
  • 69
  • 112