How to Reverse a String in C#

Reversing a string is a common programming exercise that helps developers understand arrays, loops, and string manipulation techniques. Although it may seem simple, there are multiple ways to reverse a string in C#, each with different advantages.

In this guide, you'll learn several practical methods for reversing strings and discover when each approach is most appropriate.


What Does It Mean to Reverse a String?

Reversing a string means rearranging its characters in the opposite order.

Original: Hello
            Reversed: olleH

String reversal is commonly used in coding exercises, algorithms, text processing, and interview questions.

Method 1: Using Array.Reverse()

One of the easiest ways to reverse a string is by converting it into a character array and then using the built-in Array.Reverse() method.

string text = "Hello";

                char[] characters = text.ToCharArray();

                Array.Reverse(characters);

                string reversed = new string(characters);

                Console.WriteLine(reversed);

Output

olleH

Advantages

  • Easy to understand.
  • Uses built-in .NET functionality.
  • Suitable for most situations.

Method 2: Using LINQ

LINQ provides a concise way to reverse characters in a string.

using System.Linq;

                    string text = "Hello";

                    string reversed =
                    new string(text.Reverse().ToArray());

                    Console.WriteLine(reversed);

Output

olleH

Advantages

  • Short and readable.
  • Uses modern C# syntax.
  • Useful for quick operations.

Keep in mind that LINQ may introduce additional overhead compared to lower-level approaches.

Method 3: Using a Loop

A manual loop provides full control over the reversal process and helps beginners understand how string manipulation works internally.

string text = "Hello";

                        string reversed = "";

                        for (int i = text.Length - 1; i >= 0; i--)
                        {
                        reversed += text[i];
                        }

                        Console.WriteLine(reversed);

Output

olleH

While educational, repeated string concatenation may be less efficient for very large strings.

Method 4: Using StringBuilder

For larger strings, StringBuilder can help reduce unnecessary memory allocations.

using System.Text;

                            string text = "Hello";

                            StringBuilder builder = new StringBuilder();

                            for (int i = text.Length - 1; i >= 0; i--)
                            {
                            builder.Append(text[i]);
                            }

                            string reversed = builder.ToString();

                            Console.WriteLine(reversed);

Output

olleH

Performance Comparison

Method Readability Performance Best For
Array.Reverse() Excellent Very Good General Use
LINQ Excellent Good Short Code
Loop Good Moderate Learning Purposes
StringBuilder Good Excellent Large Strings

When Would You Reverse a String?

Although string reversal is often used as a programming exercise, it can also be useful in practical situations:

  • Algorithm practice.
  • Palindrome detection.
  • Data transformation tasks.
  • Text processing applications.
  • Technical interviews.

Best Practices

  • Use Array.Reverse() for simplicity.
  • Use LINQ when readability is preferred.
  • Use StringBuilder for larger operations.
  • Avoid excessive string concatenation in loops.
  • Choose the method that best fits your application's needs.

Frequently Asked Questions

Which method is fastest?

Array.Reverse() and StringBuilder-based approaches generally provide strong performance for most scenarios.

Can I reverse a string without converting it to a character array?

Yes. LINQ and loop-based solutions can reverse strings without directly calling Array.Reverse().

Is LINQ recommended for string reversal?

LINQ is convenient and readable, though it may not always be the most efficient option for performance-critical applications.

Why are strings immutable in C#?

String immutability improves reliability, thread safety, and memory management within the .NET runtime.

Conclusion

There are several ways to reverse a string in C#, including Array.Reverse(), LINQ, loops, and StringBuilder. Each technique has strengths depending on your goals and the size of the data being processed.

Understanding these approaches improves your knowledge of string manipulation and helps build a stronger foundation in C# programming.