Array.Reverse

Reverse String
Reverse String in C#
using System;

public static class ReverseString
{
    public static string Reverse(string input)
    {
        var chars = input.ToCharArray();
        Array.Reverse(chars);
        return new string(chars);
    }
}

The string class' ToCharArray() method returns the string's characters as a char[].

Caution

The char[] returned by ToCharArray() is a copy of the string's characters. Modifying the values in the char[] does not update the string it was created from.

We then pass the char[] to the Array.Reverse() method, which will reverse the array's content in-place (meaning the argument is modified).

Finally, we return the reversed string by calling its constructor with the (reversed) char[].

Performance

If you're interested in how this approach's performance compares to other approaches, check the performance approach.

24th Apr 2024 · Found it useful?