Reverse Array Elements Using C#
In .Net Framework reversing array elements can be done by using Reverse method on Array type. This code snippet shows you how this method can be used to reverse an array.
// Declare an array with 10 elements int[] numbers = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; // Output elements in original order for (int i = 0; i < numbers.Length; i++) { Console.Write(" {0}", numbers[i]); } Console.WriteLine(); // Reverse the array Array.Reverse(numbers); // Output elements with changed order for (int i = 0; i < numbers.Length; i++) { Console.Write(" {0}", numbers[i]); } Console.ReadKey();
Here is the output.
Array.Reverse method also provides an overload which can be used to reverse elements at a given indexes. For example we want to reverse elements 3, 4 and 5 but leave others intact. This can be achieved using the following code snippet.
// Declare an array with 10 elements int[] numbers = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; // Output elements in original order for (int i = 0; i < numbers.Length; i++) { Console.Write(" {0}", numbers[i]); } Console.WriteLine(); // Reverse numbers 3 , 4 and 5 Array.Reverse(numbers, 2, 3); // Output elements with changed order for (int i = 0; i < numbers.Length; i++) { Console.Write(" {0}", numbers[i]); } Console.ReadKey();
And the output is.

Tagged with: Code Snippets
5 Responses to Reverse Array Elements Using C#
Leave a Reply Cancel reply
Top Posts
- LINQ To SQL Tutorial
- LINQ To SQL Join On Multiple Conditions
- Code Sample: Programmatically Download File Using C#
- Windows 7 Control Panel In Classic Mode
- More Details Emerge On Microsoft Master Certification
- Use SqlConnection With LINQ To SQL
- Free Icons And Images With Visual Studio 2008
- Capture XML In WCF Service
- Dynamic Sort With LINQ
- StyleCop Tutorial
Tags
.Net 2010 ADO.NET ASP.NET Azure Blogging Books Browsers C# Certification Cloud Computing Code Snippets Community Data Services Eclipse Entity Framework Google IDE Java LINQ Mac Microsoft Museum NetBeans Office Oracle REST SharePoint Silverlight SQL Server T-SQL Tips Tools Training Visual Studio Visual Studio 2010 WCF Web Windows Windows 7 Windows Forms Windows Live WMI WPF XAML


Hi there, I understand on how to reverse the array. But how can you Rearrange the numbers in the array so that the first becomes the last, etc.
I can understand you can physically swap each array around but what if you let the user to choose how big the array is going to be.
cheers
Lee
In order to cmplete this, can I cahnge the for loop to assign the first entered numbers to the end of the array, thus automatically reversing the entered numbers? if so how do I do this?
I have managed to do it now. Basically I have reversed the original array then copied it to a newly created array then used for each to show the new array thus swapping all of the values around so the last original value is now the 1 st value
Hi Lee,
Sorry if I misunderstood but isn’t that what Array.Reverse is doing?
it is yeah, but my lecturer wanted us to read in numbers
and then reverse them without using Array.reverse and permemently keeping them in the reverse order in the array.
He wasnt happy with the way I have done it by creating a new aray, soI’ve had to do it all over again