简体   繁体   中英

C# - Cannot implicitly convert type `string[]' to `string'

Trying to take a string, split it by spaces, reverse it, and return the array values.

I have looked at other answers that say you can just return the array like this:

public class Program
    {
        public static string NameShuffle(string str)
        {
                    string[] strArray = str.Split(' ');
                    Array.Reverse( strArray );
                    return strArray;
        }
    }

But for some reason I am getting an error:

Cannot implicitly convert type string[] to string

Your return type on the method is string and it should be string[]

public static string[] NameShuffle(string str)
//             ^HERE^
{
    string[] strArray = str.Split(' ');
    Array.Reverse(strArray);
    return strArray;
}

if you need to convert it back to a string then do something like this

public static string NameShuffle(string str)
{
    string[] strArray = str.Split(' ');
    Array.Reverse(strArray);
    return string.Join(' ', strArray);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM