Create Reverse Iterator : Generic List « Generic « C# / CSharp Tutorial






using System;
using System.Collections.Generic;

public class MainClass
{
    static void Main() {
        List<int> intList = new List<int>();
        intList.Add( 1 );
        intList.Add( 2 );
        intList.Add( 3 );
        intList.Add( 4 );

        foreach( int n in CreateReverseIterator(intList) ) {
            Console.WriteLine( n );
        }
    }

    static IEnumerable<T> CreateReverseIterator<T>( IList<T> list ) {
        int count = list.Count;
        for( int i = count-1; i >= 0; --i ) {
            yield return list[i];
        }
    }
}
4
3
2
1








18.2.Generic List
18.2.1.Use generic list to store your own class
18.2.2.Create Reverse Iterator
18.2.3.Add value to Generic List and display the array list using array indexing
18.2.4.Remove elements from generic List
18.2.5.Use foreach loop to display the generic list
18.2.6.List Capacity and Element Count
18.2.7.Change contents in a generic List using array indexing
18.2.8.Store user-defined Objects in a List collection