
You want to reverse the ordering of all of the elements in your array using the C# programming language. Although this task could be accomplished with a for loop, the Array.Reverse method is more convenient and also easier to read. Array.Reverse inverts the ordering of the elements in any array.

First, please notice that the Array.Reverse method is a static method on the Array type. You should pass one argument to the method: the reference to the array you want to reverse. In this example, an integer array is used. The Array.Reverse method is invoked twice, and this reverses the original array, and then reverses the reversed array, yielding the original order.
This C# program demonstrates Array.Reverse. Reverse inverts the order of elements.
Program that uses Array.Reverse method [C#]
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);
}
}
}
Output
1
2
3
3
2
1
1
2
3
So how does the Array.Reverse method work? In its .NET Framework 4.0 implementation, the method checks the parameter and then invokes TrySZReverse: this particular method is implemented in native code, not managed code. Because this internal call is native, it is likely heavily optimized for performance.
Most important, though, is that it is much simpler to use Array.Reverse than to implement a custom reversal algorithm. Not only does Array.Reverse use native code, but it also makes your program simpler; it is therefore a clear win in most contexts.
Here we examined the Array.Reverse static method. This method receives on Array type parameter, such as an int[] array, and reverses the order of the elements in that same array. You do not need to allocate another array or do any manual copying of elements.
Array Types