Skip to main content

This sample shows how to implement an IEnumerable collection class that can be used with the foreach statement.

//Copyright (C) Microsoft Corporation.  All rights reserved.

// tokens.cs
using System;
// The System.Collections namespace is made available:
using System.Collections;

// Declare the Tokens class:
public class Tokens : IEnumerable
{
    private string[] elements;

    Tokens(string source, char[] delimiters)
    {
        // Parse the string into tokens:
        elements = source.Split(delimiters);
    }

    // IEnumerable Interface Implementation:
    //   Declaration of the GetEnumerator() method
    //   required by IEnumerable
    public IEnumerator GetEnumerator()
    {
        return new TokenEnumerator(this);
    }

    // Inner class implements IEnumerator interface:
    private class TokenEnumerator : IEnumerator
    {
        private int position = -1;
        private Tokens t;

        public TokenEnumerator(Tokens t)
        {
            this.t = t;
        }

        // Declare the MoveNext method required by IEnumerator:
        public bool MoveNext()
        {
            if (position < t.elements.Length - 1)
            {
                position++;
                return true;
            }
            else
            {
                return false;
            }
        }

        // Declare the Reset method required by IEnumerator:
        public void Reset()
        {
            position = -1;
        }

        // Declare the Current property required by IEnumerator:
        public object Current
        {
            get
            {
                return t.elements[position];
            }
        }
    }

    // Test Tokens, TokenEnumerator

    static void Main()
    {
        // Testing Tokens by breaking the string into tokens:
        Tokens f = new Tokens("This is a well-done program.",
                              new char[] {' ', '-'});
        foreach (string item in f)
        {
            Console.WriteLine(item);
        }
    }
}

// --------------------------------------

//Copyright (C) Microsoft Corporation.  All rights reserved.

// tokens2.cs
using System;
using System.Collections;

public class Tokens: IEnumerable
{
    private string[] elements;

    Tokens(string source, char[] delimiters)
    {
        elements = source.Split(delimiters);
    }

    // IEnumerable Interface Implementation:

    public TokenEnumerator GetEnumerator() // non-IEnumerable version
    {
        return new TokenEnumerator(this);
    }

    IEnumerator IEnumerable.GetEnumerator() // IEnumerable version
    {
        return (IEnumerator) new TokenEnumerator(this);
    }

    // Inner class implements IEnumerator interface:

    public class TokenEnumerator: IEnumerator
    {
        private int position = -1;
        private Tokens t;

        public TokenEnumerator(Tokens t)
        {
            this.t = t;
        }

        public bool MoveNext()
        {
            if (position < t.elements.Length - 1)
            {
                position++;
                return true;
            }
            else
            {
                return false;
            }
        }

        public void Reset()
        {
            position = -1;
        }

        public string Current // non-IEnumerator version: type-safe
        {
            get
            {
                return t.elements[position];
            }
        }

        object IEnumerator.Current // IEnumerator version: returns object
        {
            get
            {
                return t.elements[position];
            }
        }
    }

    // Test Tokens, TokenEnumerator

    static void Main()
    {
        Tokens f = new Tokens("This is a well-done program.",
                              new char [] {' ', '-'});
        foreach (string item in f) // try changing string to int
        {
            Console.WriteLine(item);
        }
    }
}