Home
Search
C# Array.Reverse ExampleInvoke Array.Reverse, which reverses the order of elements. The array is modified in-place.
Array.Reverse. This .NET method inverts the ordering of an array's elements. This task could be accomplished with a for-loop. But Array.Reverse() is more convenient.
For
This image shows a reverse array.
C# method info. The Array.Reverse method is a static method on the Array type. It is simple to use—we need the System namespace. It changes the array.
Array
Static
Example. You should pass one argument to Array.Reverse: the reference to the array you want to reverse. In this example, an integer array is used.
Int Array
Then Array.Reverse method is called twice. This reverses the original array, and then reverses the reversed array.
This image shows a reverse array.
using System; class Program { static void Main() { // Input array. int[] array = { 1, 2, 3 }; // Print. foreach (int value in array) { Console.WriteLine(value); } Console.WriteLine(); // Reverse. Array.Reverse(array); // Print. foreach (int value in array) { Console.WriteLine(value); } Console.WriteLine(); // Reverse again. Array.Reverse(array); // Print. foreach (int value in array) { Console.WriteLine(value); } } }
1 2 3 3 2 1 1 2 3
Usage notes. Array.Reverse is easier to use than a custom reversal algorithm. Not only does Array.Reverse use optimized logic, but it also makes your program simpler. It is an improvement.
A summary. Here we examined the Array.Reverse static method. This method receives an Array type parameter, such as an int array, and reverses the order of the elements in that same array.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on 8/29/2021 (image).
Home
© 2007-2022 sam allen.
see site info on the changelog.