code_junkie

Does Class need to implement IEnumerable to use Foreach

Nov 11th, 2011
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. class EnumerableWrapper {
  2. private readonly TheObjectType obj;
  3.  
  4. public EnumerableWrapper(TheObjectType obj) {
  5. this.obj = obj;
  6. }
  7.  
  8. public IEnumerator<YourType> GetEnumerator() {
  9. return obj.TheMethodReturningTheIEnumerator();
  10. }
  11. }
  12.  
  13. // Called like this:
  14.  
  15. foreach (var xyz in new EnumerableWrapper(yourObj))
  16. …;
  17.  
  18. foreach (var yz in yourObj.MethodA())
  19. …;
  20.  
  21. using System;
  22. using System.Collections.Generic;
  23. using System.Text;
  24.  
  25. namespace ConsoleApplication3
  26. {
  27. class FakeIterator
  28. {
  29. int _count;
  30.  
  31. public FakeIterator(int count)
  32. {
  33. _count = count;
  34. }
  35. public string Current { get { return "Hello World!"; } }
  36. public bool MoveNext()
  37. {
  38. if(_count-- > 0)
  39. return true;
  40. return false;
  41. }
  42. }
  43.  
  44. class FakeCollection
  45. {
  46. public FakeIterator GetEnumerator() { return new FakeIterator(3); }
  47. }
  48.  
  49. class Program
  50. {
  51. static void Main(string[] args)
  52. {
  53. foreach (string value in new FakeCollection())
  54. Console.WriteLine(value);
  55. }
  56. }
  57. }
  58.  
  59. foreach (type identifier in expression) statement
  60.  
  61. class ForeachWrapper
  62. {
  63. private IEnumerator _enumerator;
  64.  
  65. public ForeachWrapper(Func<IEnumerator> enumerator)
  66. {
  67. _enumerator = enumerator;
  68. }
  69.  
  70. public IEnumerator GetEnumerator()
  71. {
  72. return _enumerator();
  73. }
  74. }
  75.  
  76. foreach (var element in new ForeachWrapper(x => myClass.MyEnumerator()))
  77. {
  78. ...
  79. }
  80.  
  81. class Counter
  82. {
  83. public IEnumerable<int> Count(int max)
  84. {
  85. int i = 0;
  86. while (i <= max)
  87. {
  88. yield return i;
  89. i++;
  90. }
  91. yield break;
  92. }
  93. }
  94.  
  95. Counter cnt = new Counter();
  96.  
  97. foreach (var i in cnt.Count(6))
  98. {
  99. Console.WriteLine(i);
  100. }
  101.  
  102. class EnumerableAdapter
  103. {
  104. ExternalSillyClass _target;
  105.  
  106. public EnumerableAdapter(ExternalSillyClass target)
  107. {
  108. _target = target;
  109. }
  110.  
  111. public IEnumerable GetEnumerator(){ return _target.SomeMethodThatGivesAnEnumerator(); }
  112.  
  113. }
  114.  
  115. foreach (object y in X.A())
  116. {
  117. //...
  118. }
  119.  
  120. // or
  121.  
  122. foreach (object y in X.B())
  123. {
  124. //...
  125. }
  126.  
  127. public class Foo
  128. {
  129. private int[] _someInts = { 1, 2, 3, 4, 5, 6 };
  130. public IEnumerator GetEnumerator()
  131. {
  132. foreach (var item in _someInts)
  133. {
  134. yield return item;
  135. }
  136. }
  137. }
  138.  
  139. public IEnumerator GetEnumerator()
  140. {
  141. return _someInts.GetEnumerator();
  142. }
  143.  
  144. foreach (int item in new Foo())
  145. {
  146. Console.Write("{0,2}",item);
  147. }
Add Comment
Please, Sign In to add comment