C# .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.

  • Jumpstart your career with this skill path, first by learning the C# language, then building web apps with ASP.NET Core and the Razor Pages.
    • Includes 4 Courses
    • With Certificate
    • Intermediate.
      44 hours
  • Learn Microsoft's popular C# programming language, used to make websites, mobile apps, video games, VR, and more.
    • Beginner Friendly.
      15 hours

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

  • Jumpstart your career with this skill path, first by learning the C# language, then building web apps with ASP.NET Core and the Razor Pages.
    • Includes 4 Courses
    • With Certificate
    • Intermediate.
      44 hours
  • Learn Microsoft's popular C# programming language, used to make websites, mobile apps, video games, VR, and more.
    • Beginner Friendly.
      15 hours