Skip to content

Instantly share code, notes, and snippets.

@joergbattermann
Created February 7, 2011 13:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joergbattermann/814352 to your computer and use it in GitHub Desktop.
Save joergbattermann/814352 to your computer and use it in GitHub Desktop.
WordEnumerable.cs
using System.Collections;
using System.Collections.Generic;
namespace WordEnumerable
{
public class WordEnumerable : IEnumerable<string>
{
/// <summary>
/// Stores initial text for GetEnumerator() / enumerator re-creation
/// </summary>
private readonly string _text;
/// <summary>
/// Initializes a new instance of the <see cref="WordEnumerable"/> class.
/// </summary>
/// <param name="text">The text.</param>
public WordEnumerable(string text = null)
{
_text = text;
}
#region Implementation of IEnumerable
/// <summary>
/// Returns an enumerator that iterates through the collection.
/// </summary>
/// <returns>
/// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
/// </returns>
/// <filterpriority>1</filterpriority>
public IEnumerator<string> GetEnumerator()
{
// originally I reused and returned one and the same enumerator here and this was causing the problem.
// Creating a fresh Enumerator each time this is called fixed it
return new WordEnumerator(_text);
}
/// <summary>
/// Returns an enumerator that iterates through a collection.
/// </summary>
/// <returns>
/// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
/// </returns>
/// <filterpriority>2</filterpriority>
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment