Will this always print elements in the same order on all platforms?
foreach (var el in multidimArray) Console.WriteLine(el);
I'm looking for a quote from the specifications or anything official.
Yes, the enumeration order of built-in arrays is well-defined and platform-independent.
See the relevant part of the C# Specification (8.8.4 The foreach statement):
The order in which foreach traverses the elements of an array, is as follows:
- For single-dimensional arrays elements are traversed in increasing index order, starting with index 0 and ending with index Length – 1.
- For multi-dimensional arrays, elements are traversed such that the indices of the rightmost dimension are increased first, then the next left dimension, and so on to the left.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With