Advertisement
_CodeBehind

class ListyIterator<T>

Jul 27th, 2017
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.41 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class ListyIterator<T> : IEnumerable<T>
  6. {
  7.     public int CurrentIndex { get; private set; }
  8.     private readonly IList<T> dataList;
  9.  
  10.     public ListyIterator(params T[] items)
  11.     {
  12.         this.CurrentIndex = 0;
  13.         this.dataList = new List<T>(items);
  14.     }
  15.  
  16.     public bool Move()
  17.     {
  18.         if (this.CurrentIndex < this.dataList.Count - 1)
  19.         {
  20.             ++this.CurrentIndex;
  21.             return true;
  22.         }
  23.         return false;
  24.     }
  25.  
  26.     public bool HasNext()
  27.     {
  28.         if (this.CurrentIndex < this.dataList.Count - 1)
  29.         {
  30.             return true;
  31.         }
  32.         return false;
  33.     }
  34.  
  35.     public string  Print()
  36.     {
  37.         return this.dataList.Count == 0
  38.             ? throw new InvalidOperationException("Invalid Operation!")
  39.             :$"this.dataList[this.CurrentIndex]";
  40.     }
  41.  
  42.     public string PrintAll()
  43.     {
  44.         return this.dataList.Count == 0
  45.             ? throw new InvalidOperationException("Invalid Operation!")
  46.             : $"{string.Join(" ", this.dataList)}";
  47.     }
  48.  
  49.     public IEnumerator<T> GetEnumerator()
  50.     {
  51.         for (int i = 0; i < this.dataList.Count -1; i++)
  52.         {
  53.             yield return this.dataList[i];
  54.         }
  55.     }
  56.  
  57.     IEnumerator IEnumerable.GetEnumerator()
  58.     {
  59.         return this.GetEnumerator();
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement