Questions tagged [enumeration]

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

Use enums for enumeration types.

1779 questions
4012
votes
32 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
  • 220,884
  • 228
  • 805
  • 1,125
1216
votes
38 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
  • 63,711
  • 30
  • 82
  • 93
860
votes
16 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
  • 11,218
  • 5
  • 26
  • 35
818
votes
15 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
  • 63,705
  • 37
  • 85
  • 98
458
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
  • 49,211
  • 44
  • 120
  • 177
279
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,…
258
votes
8 answers

What does the "map" method do in Ruby?

I'm new to programming. Can someone explain what .map would do in: params = (0...param_count).map
bigpotato
  • 22,922
  • 46
  • 147
  • 286
241
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
  • 65,227
  • 26
  • 112
  • 160
184
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,026
  • 9
  • 27
  • 36
183
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
  • 1,843
  • 2
  • 11
  • 5
183
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
  • 33,381
  • 26
  • 125
  • 160
171
votes
12 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
  • 12,834
  • 9
  • 53
  • 84
151
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
  • 6,928
  • 4
  • 13
  • 29
150
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
  • 33,265
  • 24
  • 58
  • 70
127
votes
10 answers

Difference between Java Enumeration and Iterator

What is the exact difference between these two interfaces? Does Enumeration have benefits over using Iterator? If anyone could elaborate, a reference article would be appreciated.
cometta
  • 32,613
  • 72
  • 206
  • 316
1
2 3
99 100