Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does multidimensional array enumerator guarantee the same order for all implementations?

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.

like image 219
Vlad Avatar asked Dec 13 '25 15:12

Vlad


1 Answers

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.
like image 94
Theodoros Chatzigiannakis Avatar answered Dec 16 '25 04:12

Theodoros Chatzigiannakis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!