.Reverse()

CaupolicanDiaz's avatar
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 reversed
  • index, an integer specifying the start of the subset
  • length, 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.

Code
Output
Loading...

All contributors

Contribute to Docs

Learn C# on Codecademy