C# program to Reverse an array
To reverse an array in C#, you can use the built-in Array.Reverse method. Here's an example program that reverses an array using the Array.Reverse method:
using System;
public class Program {
public static void Main(string[] args) {
int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
// Reverse the array using Array.Reverse
Array.Reverse(numbers);
Console.WriteLine("Reversed array:");
foreach (int num in numbers) {
Console.Write(num + " ");
}
}
}
In this program, we have an array of integers called numbers with the values 1 to 9. We use the Array.Reverse method to reverse the array. The Array.Reverse method modifies the original array in-place, reversing the order of its elements.
The program outputs the reversed array:
Reversed array:
9 8 7 6 5 4 3 2 1
By utilizing the Array.Reverse method, you can easily reverse the elements of an array without implementing a custom reversal algorithm.