C# loop – break vs. continue

break will exit the loop completely, continue will just skip the current iteration. For example: for (int i = 0; i < 10; i++) { if (i == 0) { break; } DoSomeThingWith(i); } The break will cause the loop to exit on the first iteration – DoSomeThingWith will never be executed. This here: for … Read more

foreach vs someList.ForEach(){}

There is one important, and useful, distinction between the two. Because .ForEach uses a for loop to iterate the collection, this is valid (edit: prior to .net 4.5 – the implementation changed and they both throw): someList.ForEach(x => { if(x.RemoveMe) someList.Remove(x); }); whereas foreach uses an enumerator, so this is not valid: foreach(var item in … Read more

Which Typesafe Enum in C++ Are You Using?

I’m currently playing around with the Boost.Enum proposal from the Boost Vault (filename enum_rev4.6.zip). Although it was never officially submitted for inclusion into Boost, it’s useable as-is. (Documentation is lacking but is made up for by clear source code and good tests.) Boost.Enum lets you declare an enum like this: BOOST_ENUM_VALUES(Level, const char*, (Abort)(“unrecoverable problem”) … Read more

Enumerate or list all variables in a program of [your favorite language here] [closed]

In python, using locals which returns a dictionary containing all the local bindings, thus, avoiding eval: >>> foo1 = “Hello world” >>> foo2 = “bar” >>> foo3 = {“1″:”a”, … “2”:”b”} >>> foo4 = “1+1” >>> import pprint >>> pprint.pprint(locals()) {‘__builtins__’: <module ‘__builtin__’ (built-in)>, ‘__doc__’: None, ‘__name__’: ‘__main__’, ‘foo1’: ‘Hello world’, ‘foo2’: ‘bar’, ‘foo3’: {‘1’: … Read more

Collection was modified; enumeration operation may not execute in ArrayList [duplicate]

You are removing the item during a foreach, yes? Simply, you can’t. There are a few common options here: use List<T> and RemoveAll with a predicate iterate backwards by index, removing matching items for(int i = list.Count – 1; i >= 0; i–) { if({some test}) list.RemoveAt(i); } use foreach, and put matching items into … Read more

Case objects vs Enumerations in Scala

One big difference is that Enumerations come with support for instantiating them from some name String. For example: object Currency extends Enumeration { val GBP = Value(“GBP”) val EUR = Value(“EUR”) //etc. } Then you can do: val ccy = Currency.withName(“EUR”) This is useful when wishing to persist enumerations (for example, to a database) or … Read more