Compilation and execution: csc tokens2.cs
Tokens2
Tokens2.cs: // Copyright (c) Microsoft Corporation. All Rights Reserved.
// tokens2.csusing 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 genumerator () // 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 = -1; private tokens t;
Public tokenenumerator (tokens t) {this.t = t;}
Public Bool MoveNext () {ix (position 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 [] {'', '-'}); Forech (String Item In f) // Try Changing String To INT {console.writeLine (item);}}}