#600 – Reversing the Elements in an Array
June 7, 2012 Leave a comment
You can use the static Array.Reverse method to reverse the order of elements in an array. This modifies the contents of the array that you pass to Array.Reverse.
int[] someNumbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; DumpArrayContents(someNumbers); // Reverse the elements using Array.Reverse Array.Reverse(someNumbers); DumpArrayContents(someNumbers);
Note that this Reverse method is different from the IEnumerable<int>.Reverse method, which does not modify the contents of the array, but just returns a reversed sequence.
// IEnumerable<int>.Reverse int[] moreNumbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; IEnumerable<int> reversedNums = moreNumbers.Reverse(); DumpArrayContents(moreNumbers); DumpArrayContents(reversedNums);