简体   繁体   English

C# 调用反向时出错:System.Linq.Enumerable+ReverseIterator`1[System.Char]

[英]C# Error when calling Reverse : System.Linq.Enumerable+ReverseIterator`1[System.Char]

a quite simple code:一个非常简单的代码:

string str = "hello  world";
            
Console.WriteLine(str.GetType());

Console.WriteLine("str.Reverse().ToString():");
Console.WriteLine(str.Reverse().ToString());

and got the following output:并得到以下 output:

System.String str.Reverse().ToString(): System.Linq.Enumerable+ReverseIterator`1[System.Char] System.String str.Reverse().ToString(): System.Linq.Enumerable+ReverseIterator`1[System.Char]

the question is, why there is the 'ReverseIterator`1' error?问题是,为什么会出现“ReverseIterator`1”错误? thanks.谢谢。

String does not have instance method Reverse , it is actually an extension method Enumerable.Reverse<TSource>(IEnumerable<TSource>) available on string due to the fact that it implements IEnumerable<char> : String没有实例方法Reverse ,它实际上是字符串上可用的扩展方法Enumerable.Reverse<TSource>(IEnumerable<TSource>) ,因为它实现了IEnumerable<char>

public sealed class String : ICloneable, 
    IComparable, 
    IComparable<string>, 
    IConvertible, 
    IEquatable<string>, 
    System.Collections.Generic.IEnumerable<char>

Reverse returns IEnumerable<char> with underlying implementation missing overload for ToString so it outputs type name. Reverse返回IEnumerable<char> ,其中底层实现缺少ToString的重载,因此它输出类型名称。 Ie next to lines will have the same output:即旁边的行将具有相同的 output:

Console.WriteLine(str.Reverse().ToString());
Console.WriteLine(str.Reverse().GetType());

You can reverse string next "quick and dirty" solution:您可以在下一个“快速而肮脏”的解决方案中反转字符串:

var reversed = new string(str.Reverse().ToArray());

Or select one of answers provided here .或 select 此处提供的答案之一。

String does not have instance method Reverse . String没有实例方法Reverse Your code str.Reverse().ToString() is actually splitting your string char-by-char and then reversing it using the Reverse method in the Array instance.您的代码str.Reverse().ToString()实际上是逐个字符地拆分字符串,然后使用Array实例中的Reverse方法将其反转。

What you can do is manually split your string into a char array, reverse the array using the Reverse method in the Array instance and then join it using the Join method in String instance.您可以做的是手动将您的字符串拆分为一个 char 数组,使用Array实例中的Reverse方法反转该数组,然后使用String实例中的Join方法将其连接起来。 Something like this:像这样的东西:

string str = "hello  world";

Console.WriteLine(str.GetType());

// Manually split the string into a char array,
// reverse the array and then join it.
Console.WriteLine("str.ToCharArray().Reverse():");
string NewStr = string.Join("", str.ToCharArray().Reverse());

Console.WriteLine(NewStr);
// Output: "dlrow  olleh"

But if you want to stick with the code you wrote then you can do something like this:但是,如果您想坚持使用您编写的代码,那么您可以执行以下操作:

string str = "hello  world";

Console.WriteLine(str.GetType());

string NewStr = "";
Console.WriteLine("str.Reverse().ToString():");

// Split the string into a char array,
// reverse the char array and append the chars to a new string.
foreach (var i in str.Reverse())
{
    NewStr += i.ToString();
}

Console.WriteLine(NewStr);
// Output: "dlrow  olleh"

Hope it helped:)希望它有所帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C# 中的 System.Collections.Generic.List`1[System.Char] - System.Collections.Generic.List`1[System.Char] in C# Array.ToString() 返回 System.Char[] c# - Array.ToString() returning System.Char[] c# Linq /删除空格返回实际值“ system.char” - Linq / Remove whitespace returns actual value 'system.char' 为什么用于Unicode属性测试的C#System.Char方法有两个重载? - Why do C# System.Char methods for Unicode property tests have two overloads? C#成员&#39;System.Char [] System.String :: ToCharArray()&#39;在另一个模块中声明,需要导入到Mono.Cecil中 - C# Member 'System.Char[] System.String::ToCharArray()' is declared in another module and needs to be imported in Mono.Cecil Enumerable.First(System.Linq)C#的替代方法 - an alternative to Enumerable.First (System.Linq) C# ASP.NET C#System.Linq.Enumerable + WhereSelectEnumerable错误 - ASP.NET C# System.Linq.Enumerable+WhereSelectEnumerable Error Automapper 错误:System.InvalidOperationException:'缺少从 System.String 到 System.Char 的 map - Automapper error: System.InvalidOperationException: 'Missing map from System.String to System.Char 获取System.Char []而不是值 - Getting System.Char[] instead of value 方法系统linq的类型参数可枚举为可枚举-错误 - the type arguments for method system linq enumerable as enumerable - Error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM