Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- namespace SimpleIEnumerableExploded
- {
- class Program
- {
- static void Main(string[] args)
- {
- //Lets get the 'IEnumerable Class' that RandomNum gets compiled down into.
- var IEnumeratorClass = new RandomNumIEnumerableExploded(10, 10);
- //All an IEnumerable is is a class with 'GetEnumerator'... so lets get it!
- var IEnumerableClass = ((IEnumerable<int>)IEnumeratorClass).GetEnumerator();
- //It can be used like so:
- while (IEnumerableClass.MoveNext()) Console.WriteLine(IEnumerableClass.Current);
- Console.ReadLine();
- }
- static Random rnd = new Random();
- public class RandomNumIEnumerableExploded : IEnumerable<int>
- {
- int max, count;
- Random _rnd;
- public RandomNumIEnumerableExploded(int max, int count)
- {
- this.max = max;
- this.count = count;
- _rnd = rnd;
- }
- IEnumerator IEnumerable.GetEnumerator()
- {
- return new RandomNumIEnumeratorExploded(max, count, rnd);
- }
- IEnumerator<int> IEnumerable<int>.GetEnumerator()
- {
- return new RandomNumIEnumeratorExploded(max, count, rnd);
- }
- }
- public class RandomNumIEnumeratorExploded : IEnumerator<int>
- {
- int max, count;
- Random _rnd;
- int current;
- int currentCount = 0;
- public RandomNumIEnumeratorExploded(int max, int count, Random rnd)
- {
- this.max = max;
- this.count = count;
- _rnd = rnd;
- }
- int IEnumerator<int>.Current { get { return current; } }
- object IEnumerator.Current { get { return current; } }
- public bool MoveNext()
- {
- if (currentCount < count)
- {
- currentCount++;
- current = rnd.Next(max);
- return true;
- }
- return false;
- }
- public void Reset() { currentCount = 0; }
- public void Dispose() { }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement