.Reverse()
Published Feb 16, 2023Updated Apr 11, 2023
Contribute to Docs
The .Reverse()
method reverses the sequence of a subset of the elements in a one-dimensional array.
Syntax
Array.Reverse(sourceArray, index, length);
.Reverse()
takes the following parameters:
sourceArray
, the array to be reversedindex
, an integer specifying the start of the subsetlength
, an integer specifying the number of elements of the subset
If the method is run without specifying and index and length, then the entire array will be reversed.
Example
The following example initializes spamLetters
with values, and reverses the sequence:
using System;public class Example{public static void Main(string[] args){string[] spamLetters = {"S", "P", "A", "M"};Array.Reverse(spamLetters);foreach (string s in spamLetters){System.Console.Write(s + " ");}}}
The code returns the following output:
M A P S
Codebyte Example
The following example uses the .Reverse()
method to flip the final 4 characters of an array.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.