Advertisement
NPSF3000

SimpleIEnumerableExploded

Aug 1st, 2012
557
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.35 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. namespace SimpleIEnumerableExploded
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             //Lets get the 'IEnumerable Class' that RandomNum gets compiled down into.
  12.             var IEnumeratorClass = new RandomNumIEnumerableExploded(10, 10);
  13.  
  14.             //All an IEnumerable is is a class with 'GetEnumerator'... so lets get it!
  15.             var IEnumerableClass = ((IEnumerable<int>)IEnumeratorClass).GetEnumerator();
  16.  
  17.             //It can be used like so:
  18.             while (IEnumerableClass.MoveNext()) Console.WriteLine(IEnumerableClass.Current);
  19.  
  20.             Console.ReadLine();
  21.         }
  22.  
  23.         static Random rnd = new Random();
  24.  
  25.         public class RandomNumIEnumerableExploded : IEnumerable<int>
  26.         {
  27.             int max, count;
  28.             Random _rnd;
  29.             public RandomNumIEnumerableExploded(int max, int count)
  30.             {
  31.                 this.max = max;
  32.                 this.count = count;
  33.                 _rnd = rnd;
  34.             }
  35.  
  36.             IEnumerator IEnumerable.GetEnumerator()
  37.             {
  38.                 return new RandomNumIEnumeratorExploded(max, count, rnd);
  39.             }
  40.  
  41.             IEnumerator<int> IEnumerable<int>.GetEnumerator()
  42.             {
  43.                 return new RandomNumIEnumeratorExploded(max, count, rnd);
  44.             }
  45.  
  46.         }
  47.         public class RandomNumIEnumeratorExploded : IEnumerator<int>
  48.         {
  49.             int max, count;
  50.             Random _rnd;
  51.             int current;
  52.             int currentCount = 0;
  53.             public RandomNumIEnumeratorExploded(int max, int count, Random rnd)
  54.             {
  55.                 this.max = max;
  56.                 this.count = count;
  57.                 _rnd = rnd;
  58.             }
  59.  
  60.             int IEnumerator<int>.Current { get { return current; } }
  61.  
  62.             object IEnumerator.Current { get { return current; } }
  63.  
  64.             public bool MoveNext()
  65.             {
  66.                 if (currentCount < count)
  67.                 {
  68.                     currentCount++;
  69.                     current = rnd.Next(max);
  70.                     return true;
  71.                 }
  72.                 return false;
  73.             }
  74.  
  75.             public void Reset() { currentCount = 0; }
  76.             public void Dispose() { }
  77.         }
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement