C# -> string.Reverse(); - 在 # using System.Linq 中不起作用;

Ves*_*hev 0 c#

这个程序没有按预期工作,我不知道为什么。错误是CS0266 " Error CS0266 Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<char>' to 'string'. An explicit conversion exists (are you missing a cast?)

但是,它应该在使用 System.Linq 下正常工作;

using System;
using System.Linq;
namespace centuryyearsminutes
{
    class Program
    {
        static void Main(string[] args)
        {
            string aa = "Hello World!";
            string bb = aa.Reverse();
            Console.WriteLine(bb);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Cha*_*les 6

aa.Reverse() 只返回一个 Enumerable<char>

尝试:

string bb = new string(aa.Reverse().ToArray());
Run Code Online (Sandbox Code Playgroud)

虽然这可能是最好的方法:https :
//stackoverflow.com/a/15111719/11808788


Chr*_*ine 5

.Reverse()期待一个字符集合(docs),而不是一个字符串。如果你这样做:

string aa = "Hello World!";

var result = aa.ToCharArray().Reverse();

Console.WriteLine(new string(result.ToArray()));
Run Code Online (Sandbox Code Playgroud)

输出是!dlroW olleH

这应该按预期工作。