Chapter 18 Quick Reference


Chapter 18 Quick Reference

To

Do this

Example

Make a class enumerable, allowing it to support the foreach construct.

Implement the IEnumerable interface and provide a GetEnumerator method that returns an IEnumerator object.

public class Tree<T>:IEnumerable<T> {     ...     IEnumerator<T> GetEnumerator()     {         ...     } }

Implement an enumerator not by using an iterator.

Define an enumerator class that implements the IEnumerator interface and that provides the Current property and the MoveNext (and Reset) methods.

public class TreeEnumerator<T> : IEnumerator<T> {     ...     T Current     {         get         {             ...         }     }     bool MoveNext()     {         ...     } }

Define an enumerator by using an iterator.

Implement the enumerator by using the yield statement indicating which items should be returned and in which order.

IEnumerator<T> GetEnumerator() {    for (...)       yield return ... }




Microsoft Visual C# 2005 Step by Step
Microsoft® Visual C#® 2005 Step by Step (Step By Step (Microsoft))
ISBN: B002CKYPPM
EAN: N/A
Year: 2005
Pages: 183
Authors: John Sharp

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net