In this C# tutorial, I will explain, how to reverse a string in C# using Linq with a complete example. LINQ (Language Integrated Query) is a powerful feature in C# that allows you to perform queries on collections. It can also be used to perform a variety of transformations on strings. In this tutorial, you will learn how to reverse a string using LINQ in C#.
Reverse a string in C# using Linq
Let us now check in detail, how to reverse a string in C# using linq with a complete code for example.
To use LINQ, you need to include the necessary namespaces. For this tutorial, you’ll need:
using System;
using System.Linq;
Here’s a function that reverses a string using LINQ:
public static string ReverseStringWithLinq(string input)
{
return new string(input.ToCharArray().Reverse().ToArray());
}
The ToCharArray()
method converts the string to an array of characters. The Reverse()
method, provided by LINQ, reverses the sequence of the characters. Finally, the ToArray()
method converts the reversed sequence back to an array, which is then used to create a new string.
To test the function, create a simple application that takes a user’s input, reverses it, and then displays the reversed string in C#:
using System;
using System.Linq;
class Program
{
static void Main()
{
Console.WriteLine("Enter a string to reverse:");
string input = Console.ReadLine();
string reversed = ReverseStringWithLinq(input);
Console.WriteLine($"Reversed string: {reversed}");
}
public static string ReverseStringWithLinq(string input)
{
return new string(input.ToCharArray().Reverse().ToArray());
}
}
Once you run the code you can see the output below:

Conclusion
LINQ provides a concise and readable way to perform many operations in C#, including string manipulation. By understanding the basics of LINQ and how to chain methods together, you can easily extend this knowledge to perform more complex operations on strings and other collections in C#. And I hope you got an idea of how to reverse a string in C# using Linq.
You may also like:
- How to reverse a string in C# using inbuilt function?
- How to reverse string in C# using while loop?
- Reverse a String in C# Using a For Loop
Bijay Kumar is a renowned software engineer, accomplished author, and distinguished Microsoft Most Valuable Professional (MVP) specializing in SharePoint. With a rich professional background spanning over 15 years, Bijay has established himself as an authority in the field of information technology. He possesses unparalleled expertise in multiple programming languages and technologies such as ASP.NET, ASP.NET MVC, C#.NET, and SharePoint, which has enabled him to develop innovative and cutting-edge solutions for clients across the globe. Read more…