Strange behavior of Enumerator.MoveNext()

List<T>.GetEnumerator() returns a mutable value type (List<T>.Enumerator). You’re storing that value in the anonymous type. Now, let’s have a look at what this does: while (x.TempList.MoveNext()) { // Ignore this } That’s equivalent to: while (true) { var tmp = x.TempList; var result = tmp.MoveNext(); if (!result) { break; } // Original loop body } … Read more

How can a formcollection be enumerated in ASP.NET MVC?

Here are 3 ways to do it specifically with a FormCollection object. public ActionResult SomeActionMethod(FormCollection formCollection) { foreach (var key in formCollection.AllKeys) { var value = formCollection[key]; } foreach (var key in formCollection.Keys) { var value = formCollection[key.ToString()]; } // Using the ValueProvider var valueProvider = formCollection.ToValueProvider(); foreach (var key in valueProvider.Keys) { var value … Read more