[英]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());
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.