ExtensionMethod.NET Home of 818 C#, Visual Basic, F# and Javascript extension methods

Reverse

Reverses the order of the list that you wish to enumerate.

Source

public static IEnumerable<T> Reverse<T>(this IEnumerable<T> items)
        {
            IList<T> list = (IList<T>) items;
            
            if (list == null) 
                yield return default(T);

            for (int i = list.Count - 1; i >= 0; i--)
            {
                yield return list[i];
            }
        }

Example

List<string> list = new List<string>();
list.add("a");
list.add("b");

foreach (string item in list.Reverse<string>()
{
//todo: item
}

Author: Jonathan Crossland

Submitted on: 6 apr 2009

Language: C#

Type: System.IEnumerable<T>

Views: 4468