2

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;
    }
Coder
  • 117
  • 2
  • 13

6 Answers6

7

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()); 
silkfire
  • 24,585
  • 15
  • 82
  • 105
4

The issue is that myArray.Reverse() does not modify myArray. This method returns a IEnumerable of char with the reverse of myArray.

Try this:

var reversed = myArray.Reverse();

And then work with reversed.

afonte
  • 938
  • 9
  • 17
1

You need to store the return value in a variable:

var reversed = myArray.Reverse();

This is the signature for Reverse() method you are using, by the way, this is an extension method in the Enumerable class:

public static IEnumerable<TSource> Reverse<TSource>(this IEnumerable<TSource> source);

See the return type is an IEnumerable so you will need to store that and then work with that.

You can just do this:

public static string ReverseString(string word)
{
    return string.IsNullOrWhiteSpace(word) ? string.Empty 
        :  string.Concat(word.Reverse());
}
CodingYoshi
  • 25,467
  • 4
  • 62
  • 64
1

Try Array.Reverse(myArray) instead of myArray.Reverse().

You are confusing Enumerable.Reverse and Array.Reverse methods.

IEnumerable<char>.Reverse: Returns a new reversed array without changing the old array

myArray = myArray.Reverse();

Array.Reverse: A Void method that will reverse the input array

Array.Reverse(myArray);
Mhd
  • 2,778
  • 5
  • 22
  • 59
0

The method Reverse is a method that returns a new array (or collection) based on the original items.

public static string ReverseString(string word)
{
    char[] myArray = word.ToCharArray();
    myArray = myArray.Reverse().ToArray(); //<-- Call ToArray() method to convert back to the array and execute the deffered action.

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

    return ans;
}

This method is implemented by using deferred execution. The immediate return value is an object that stores all the information that is required to perform the action. The query represented by this method is not executed until the object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or For Each in Visual Basic.

Unlike OrderBy, this sorting method does not consider the actual values themselves in determining the order. Rather, it just returns the elements in the reverse order from which they are produced by the underlying source.

Nico
  • 12,493
  • 5
  • 42
  • 62
0

You should do Array.Reverse(myArray)

BipinR
  • 473
  • 7
  • 19