IList
test.Reverse(); //it's failure
Works just fine for me:
Microsoft (R) Roslyn C# Compiler version 2.1.0.61520
Loading context from 'CSharpInteractive.rsp'.
Type "#help" for more information.
> var t = new List<int> { 0, 4, 9 };
> t.Reverse();
> t
List<int>(3) { 9, 4, 0 }
>
I declare the t: IList
So try this. Thank you!
IList<int> t = new List<int>{0,4,9}
This should work:
C#
IList<int> t = new List<int>{0,4,9};
foreach (var item in t.Reverse())
{
Console.WriteLine(item);
}
List<T>.Reverse() actually mutates the list.IList<T> doesn't have a Reverse() method, you're calling Enumerable.Reverse() which returns a reversed IEnumerable<T> but doesn't change the underlying list. That's likely what you're running into.It's a pretty common problem people hit http://stackoverflow.com/questions/12390971/why-there-is-two-completely-different-version-of-reverse-for-list-and-ienumerabl.
Most helpful comment
This should work:
C# IList<int> t = new List<int>{0,4,9}; foreach (var item in t.Reverse()) { Console.WriteLine(item); }List<T>.Reverse()actually mutates the list.IList<T>doesn't have aReverse()method, you're callingEnumerable.Reverse()which returns a reversedIEnumerable<T>but doesn't change the underlying list. That's likely what you're running into.It's a pretty common problem people hit http://stackoverflow.com/questions/12390971/why-there-is-two-completely-different-version-of-reverse-for-list-and-ienumerabl.