Implementing IEnumerable easily

code 4 April 2008 | 0 Comments

Say that you’re implementing a linked list, and you want an enumerator: public IEnumerator<T> GetEnumerator() { return new Stream<T,Node>(first, node => node.next == null ? null : Tuple.Of(node.next, node.datum).AsNullable()); } This uses the following utility class to implement the enumerator in one line (along with some code for Tuples and an extension method for structs): [...]

Tagged in , ,