Why does IList<>.Reverse() not work like List<>().Reverse
Solution 1
IList<int>
doesn't have a Reverse
method, so it uses the extension method. The only way to use List<T>.Reverse
on your IList<int>
reference is to cast or convert it to a List<int>
. Casting will only work if you're sure that it's really a List<int>
in the first place:
IList<int> abstractList;
var concreteList = (List<int>)abstractList;
concreteList.Reverse();
Another option would be to create a List<int>
from your IList<int>
instance, rather than assuming it already is a List<int>
:
IList<int> abstractList;
var concreteList = new List<int>(abstractList);
concreteList.Reverse();
The reason that the Reverse
extension method doesn't actually affect the underlying list is because it operates on IEnumerable<T>
, which isn't necessarily writeable (none of the Enumerable
extension methods make changes to the original collection, they return a new collection).
To use this version of Reverse
, just use the product of the Reverse
call, rather than the original list:
IList<int> abstractList;
IEnumerable<int> reversedList = abstractList.Reverse();
Solution 2
In the second example, you're using an extension method against IEnumerable<T>
, and this is not mutating the original collection but rather producing a query that would result in a sequence of your original list in reverse order. That is to say, if you want to utilize the results of ilist.Reverse()
, you would say
var reversedList = iList.Reverse();
// reversedList is IEnumerable<int>
// 'list' and 'ilist' are not changed
foreach (int item in reversedList)
Console.Write(item);
Related videos on Youtube
AndreyAkinshin
Updated on May 15, 2022Comments
-
AndreyAkinshin almost 2 years
I have problem with
List<T>.Reverse()
andReverse(this IEnumerable<TSource> source)
. Look to the code:// Part 1 List<int> list = new List<int> { 1, 2, 3 }; foreach (int x in list) Console.Write(x); Console.WriteLine(); list.Reverse(); foreach (int x in list) Console.Write(x); Console.WriteLine(); list.Reverse(); // Part2 IList<int> ilist = list; foreach (int x in list) Console.Write(x); Console.WriteLine(); ilist.Reverse(); foreach (int x in ilist) Console.Write(x); Console.WriteLine(); ilist.Reverse();
My result:
123 321 123 123
because
Reverse()
-Part1 isList<T>.Reverse()
,Reverse()
-Part2 isReverse(this IEnumerable<TSource> source)
I want executeList<int>.Reverse()
in Part2 forIList<int>
. How I can do it?-
Dan Tao over 13 years
-
-
Moe Howard about 12 yearsshort, yet concise answer to the issue at hand. Helped me out. Thanks +1