- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to use the Reverse() method of array class in C#
The Reverse() method in array class reverses the sequence of the elements in the entire one-dimensional Array.
To reverse an array, just use the Array.Reverse() method −
Array.Reverse(temp);
Within the reverse method, set the elements like the following code snippet.
int[] list = { 29, 15, 30, 98}; int[] temp = list;
You can try to run the following code to implement Reverse() method in C#.
Example
using System; namespace Demo { class MyArray { static void Main(string[] args) { int[] list = { 29, 15, 30, 98}; int[] temp = list; Console.Write("Original Array: "); foreach (int i in list) { Console.Write(i + " "); } Console.WriteLine(); // reverse the array Array.Reverse(temp); Console.Write("Reversed Array: "); foreach (int i in temp) { Console.Write(i + " "); } Console.ReadKey(); } } }
Output
Original Array: 29 15 30 98 Reversed Array: 98 30 15 29
Advertisements