Implement generic IEnumerable : IEnumerable : Generics C# Source Code


Custom Search

C# Source Code » Generics » IEnumerable »

 

Implement generic IEnumerable









    


using System;
using System.Collections;
using System.Collections.Generic;

public class MyColl<T> : IEnumerable<T> {
    private T[] items;
    public MyColl( T[] items ) {
        this.items = items;
    }

    public IEnumerator<T> GetEnumerator() {
        foreach( T item in items ) {
            yield return item;
        }
    }

    IEnumerator IEnumerable.GetEnumerator() {
        return GetEnumerator();
    }

}

public class Test {
    static void Main() {
        MyColl<int> integers = new MyColl<int>( new int[] {1, 2, 3, 4} );

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

           
       
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo Generics
» IEnumerable