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

狂风中的少年 提交于 2020-01-11 07:23:26

问题


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;
    }

回答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);



回答2:


Perhaps you're confusing the method you're using with the static Array.Reverse, which indeed is 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()); 



回答3:


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.




回答4:


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());
}



回答5:


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.




回答6:


public static string ReverseString(string word)
{
    return string.Join("", word.Reverse());
}



回答7:


You should do Array.Reverse(myArray)



来源:https://stackoverflow.com/questions/42354309/how-to-reverse-an-array-using-the-reverse-method-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!