Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reverse an array using the Reverse method. C# [closed]

Tags:

arrays

c#

reverse

For some reason when I apply the reverse method, nothing changes.

public static string ReverseString(string word)
    {
        char[] myArray = word.ToCharArray();
        myArray.Reverse();

        string ans = string.Join("", myArray);

        return ans;
    }
like image 911
Coder Avatar asked Nov 26 '25 12:11

Coder


1 Answers

Perhaps you're confusing the method you're using with the static Array.Reverse, which is indeed a void method?

Array.Reverse Method (Array)


The one you're using is a LINQ extension method of IEnumerable, whose reference you can find here:

Enumerable.Reverse Method (IEnumerable)


For your specific case though, I'd use this oneliner:

return new string(word.Reverse().ToArray()); 
like image 140
silkfire Avatar answered Nov 28 '25 02:11

silkfire



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!