1
0
Files
common/AMWD.Common/Cli/EnumerableWalker.cs

59 lines
1.3 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
namespace AMWD.Common.Cli
{
/// <summary>
/// Walks through an <see cref="IEnumerable{T}"/> and allows retrieving additional items.
/// </summary>
/// <typeparam name="T"></typeparam>
internal class EnumerableWalker<T> : IEnumerable<T>
where T : class
{
private readonly IEnumerable<T> array;
private IEnumerator<T> enumerator;
/// <summary>
/// Initialises a new instance of the <see cref="EnumerableWalker{T}"/> class.
/// </summary>
/// <param name="array">The array to walk though.</param>
public EnumerableWalker(IEnumerable<T> array)
{
this.array = array ?? throw new ArgumentNullException(nameof(array));
}
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
enumerator = array.GetEnumerator();
return enumerator;
}
/// <summary>
/// Gets the enumerator.
/// </summary>
/// <returns>The enumerator.</returns>
public IEnumerator GetEnumerator()
{
enumerator = array.GetEnumerator();
return enumerator;
}
/// <summary>
/// Gets the next item.
/// </summary>
/// <returns>The next item.</returns>
public T GetNext()
{
if (enumerator.MoveNext())
{
return enumerator.Current;
}
else
{
return default;
}
}
}
}