How to cross C # LinkedList in reverse order

How can I make the equivalent of the following C ++ snippet using C # LinkedList ?

 std::list<MyClass*>::reverse_iterator itr(it); for(; itr != MyList.rend(); ++itr) 
+7
source share
1 answer

Like 1-off, something like:

 var el = list.Last; while (el != null) { // use el.Value el = el.Previous; } 

If you do this regularly, perhaps a similar iterator block to get all the values:

 public static IEnumerable<T> Reverse<T>(this LinkedList<T> list) { var el = list.Last; while (el != null) { yield return el.Value; el = el.Previous; } } 

then

 foreach(var val in list.Reverse()) { // use val } 
+16
source

All Articles