# Recursion with char[] in c#

I am trying to learn recursion and have a question involving an array that needs to be reversed. I am focusing on c# but the language probably doesn't matter a whole lot because my function is not using any libraries.

Here is my code:

char[] ReverseString(char[] s, int i = 0)
{
char[] x = s;
char temp = s[i];
s[i] = s[s.Length - (i + 1)];
s[s.Length - (i + 1)] = temp;

i++;
if (i < (s.Length) / 2)
{
ReverseString(s, i);
}
return x;
}


Any suggestions on how I should improve my function (maybe time complexity or using libraries (nuget packages)) is appreciated as well.

• The same problem has been address many times: 1, 2, 3, etc... – Peter Csala Nov 4 '20 at 9:51
• Tip: System.Linq is great when you want to do something with arrays or collections string hello = "Hello World!"; string olleh = new string(hello.Reverse().ToArray()); Output: !dlroW olleH. – aepot Nov 6 '20 at 18:40