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):

22254 questions
34 answers

How to enumerate an enum?

How can you enumerate an enum in C#? E.g. the following code does not compile: public enum Suit { Spades, Hearts, Clubs, Diamonds } public void EnumerateAllSuitsDemoMethod() { foreach (Suit suit in Suit) { …
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
32 answers

How do I cast int to enum in C#?

How do I cast an int to an enum in C#?
  • 113,627
  • 57
  • 144
  • 179
32 answers

How to get an enum value from a string value in Java

Say I have an enum which is just public enum Blah { A, B, C, D } and I would like to find the enum value of a string, for example "A" which would be Blah.A. How would it be possible to do this? Is the Enum.valueOf() the method I need? If so,…
  • 33,142
  • 18
  • 63
  • 96
31 answers

Get int value from enum in C#

I have a class called Questions (plural). In this class there is an enum called Question (singular) which looks like this. public enum Question { Role = 2, ProjectFunding = 3, TotalEmployee = 4, NumberOfServers = 5, …
  • 26,598
  • 13
  • 51
  • 66
15 answers

Comparing Java enum members: == or equals()?

I know that Java enums are compiled to classes with private constructors and a bunch of public static members. When comparing two members of a given enum, I've always used .equals(), e.g. public useEnums(SomeEnum a) { …
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
14 answers

What does the [Flags] Enum Attribute mean in C#?

From time to time I see an enum like the following: [Flags] public enum Options { None = 0, Option1 = 1, Option2 = 2, Option3 = 4, Option4 = 8 } I don't understand what exactly the [Flags] attribute does. Anyone have a good…
Brian Leahy
  • 34,677
  • 12
  • 45
  • 60
8 answers

How to loop through all enum values in C#?

This question already has an answer here: How do I enumerate an enum in C#? 26 answers public enum Foos { A, B, C } Is there a way to loop through the possible values of Foos? Basically? foreach(Foo in Foos)
  • 22,329
  • 11
  • 45
  • 56
22 answers

Create Generic method constraining T to an Enum

I'm building a function to extend the Enum.Parse concept that Allows a default value to be parsed in case that an Enum value is not found Is case insensitive So I wrote the following: public static T GetEnumFromString(string value, T…
  • 39,385
  • 37
  • 101
  • 139
31 answers

JavaScriptSerializer - JSON serialization of enum as string

I have a class that contains an enum property, and upon serializing the object using JavaScriptSerializer, my json result contains the integer value of the enumeration rather than its string "name". Is there a way to get the enum as a string in my…
Omer Bokhari
  • 57,458
  • 12
  • 44
  • 58
29 answers

Convert a string to an enum in C#

What's the best way to convert a string to an enumeration value in C#? I have an HTML select tag containing the values of an enumeration. When the page is posted, I want to pick up the value (which will be in the form of a string) and convert it to…
Ben Mills
  • 27,454
  • 14
  • 42
  • 38
43 answers

How can I represent an 'Enum' in Python?

I'm mainly a C# developer, but I'm currently working on a project in Python. How can I represent the equivalent of an Enum in Python?
John Rutherford
  • 10,704
  • 7
  • 30
  • 32
13 answers

What is a typedef enum in Objective-C?

I don't think I fundamentally understand what an enum is, and when to use it. For example: typedef enum { kCircle, kRectangle, kOblateSpheroid } ShapeType; What is really being declared here?
  • 16,291
  • 12
  • 43
  • 39
37 answers

String representation of an Enum

I have the following enumeration: public enum AuthenticationMethod { FORMS = 1, WINDOWSAUTHENTICATION = 2, SINGLESIGNON = 3 } The problem however is that I need the word "FORMS" when I ask for AuthenticationMethod.FORMS and not the id…
  • 15,740
  • 21
  • 56
  • 63
9 answers

A for-loop to iterate over an enum in Java

I have an enum in Java for the cardinal and intermediate directions: public enum Direction { NORTH, NORTHEAST, EAST, SOUTHEAST, SOUTH, SOUTHWEST, WEST, NORTHWEST } How can I write a for loop that iterates through each of…
Nick Meyer
  • 39,212
  • 14
  • 67
  • 75
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?
2 3
99 100