Questions tagged [enumeration]

The process of enumerating values, for example from some collection.

Use for enumeration types.

1894 questions
4339
votes
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
1308
votes
39 answers

Enumerations on PHP

I know that PHP doesn't yet have native Enumerations. But I have become accustomed to them from the Java world. I would love to use enums as a way to give predefined values which IDEs' auto-completion features could understand. Constants do the…
Henrik Paul
  • 66,919
  • 31
  • 85
  • 96
950
votes
19 answers

How to iterate a loop with index and element in Swift

Is there a function that I can use to iterate over an array and have both index and element, like Python's enumerate? for index, element in enumerate(list): ...
thinker3
  • 12,771
  • 5
  • 30
  • 36
867
votes
16 answers

C# loop - break vs. continue

In a C# (feel free to answer for other languages) loop, what's the difference between break and continue as a means to leave the structure of the loop, and go to the next iteration? Example: foreach (DataRow row in myTable.Rows) { if…
Seibar
  • 68,705
  • 38
  • 88
  • 99
465
votes
8 answers

How do I iterate over an NSArray?

I'm looking for the standard idiom to iterate over an NSArray. My code needs to be suitable for OS X 10.4+.
Steve McLeod
  • 51,737
  • 47
  • 128
  • 184
284
votes
7 answers

for each loop in Objective-C for accessing NSMutable dictionary

I am finding some difficulty in accessing mutable dictionary keys and values in Objective-C. Suppose I have this: NSMutableDictionary *xyz=[[NSMutableDictionary alloc] init]; I can set keys and values. Now, I just want to access each key and value,…
sagarkothari
  • 24,520
  • 50
  • 165
  • 235
274
votes
8 answers

What does the "map" method do in Ruby?

What does .map do in: params = (0...param_count).map
bigpotato
  • 26,262
  • 56
  • 178
  • 334
251
votes
14 answers

Case objects vs Enumerations in Scala

Are there any best-practice guidelines on when to use case classes (or case objects) vs extending Enumeration in Scala? They seem to offer some of the same benefits.
Alex Miller
  • 69,183
  • 25
  • 122
  • 167
223
votes
12 answers

How to get the name of enumeration value in Swift?

If I have an enumeration with raw Integer values: enum City: Int { case Melbourne = 1, Chelyabinsk, Bursa } let city = City.Melbourne How can I convert a city value to a string Melbourne? Is this kind of a type name introspection available in…
Evgenii
  • 36,389
  • 27
  • 134
  • 170
217
votes
6 answers

Methods inside enum in C#

In Java, it's possible to have methods inside an enum. Is there such possibility in C# or is it just a string collection and that's it? I tried to override ToString() but it does not compile. Does someone have a simple code sample?
Eya
  • 2,183
  • 2
  • 12
  • 5
208
votes
13 answers

foreach vs someList.ForEach(){}

There are apparently many ways to iterate over a collection. Curious if there are any differences, or why you'd use one way over the other. First type: List someList = foreach(string s in someList) {
Bryce Fischer
  • 5,336
  • 9
  • 30
  • 36
190
votes
13 answers

Search for a string in Enum and return the Enum

I have an enumeration: public enum MyColours { Red, Green, Blue, Yellow, Fuchsia, Aqua, Orange } and I have a string: string colour = "Red"; I want to be able to return: MyColours.Red from: public MyColours…
Matt Clarkson
  • 14,106
  • 10
  • 57
  • 85
172
votes
2 answers

Get enumeration name by value

Are there any standard methods to get Enumeration names by value? An example: class Example(enum.Enum): one = 1 two = 2 ex_variable = 1 Given ex_variable, can I obtain the string contained in Example.one.name?
Jiloc
  • 3,338
  • 3
  • 24
  • 38
153
votes
10 answers

What is the tilde (~) in the enum definition?

I'm always surprised that even after using C# for all this time now, I still manage to find things I didn't know about... I've tried searching the internet for this, but using the "~" in a search isn't working for me so well and I didn't find…
hugoware
  • 35,731
  • 24
  • 60
  • 70
152
votes
2 answers

Why is the enumeration value from a multi dimensional array not equal to itself?

Consider: using System; public class Test { enum State : sbyte { OK = 0, BUG = -1 } static void Main(string[] args) { var s = new State[1, 1]; s[0, 0] = State.BUG; State a = s[0, 0]; Console.WriteLine(a…
shingo
  • 18,436
  • 5
  • 23
  • 42
1
2 3
99 100