Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 30th, 2012  |  syntax: None  |  size: 3.15 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Manually increment an enumerator inside foreach loop
  2. foreach (DateTime time in times)
  3.         {
  4.             while (condition)
  5.             {
  6.                 // perform action
  7.                 // move to next item
  8.                 (time as IEnumerator<DateTime>).MoveNext(); // will not let me do this
  9.             }
  10.  
  11.             // code to execute after while condition is met
  12.          }
  13.        
  14. using (var enumerator = times.GetEnumerator())
  15. {
  16.     DateTime time;
  17.     while (enumerator.MoveNext())
  18.     {
  19.         time = enumerator.Current;
  20.         // pre-condition code
  21.         while (condition)
  22.         {
  23.             if (enumerator.MoveNext())
  24.             {
  25.                 time = enumerator.Current;
  26.                 // condition code
  27.             }
  28.             else
  29.             {
  30.                 condition = false;
  31.             }
  32.         }
  33.         // post-condition code
  34.     }
  35. }
  36.        
  37. foreach (DateTime time in times)      
  38. {    
  39.      if (condition)
  40.      {
  41.              // perform action
  42.              continue;
  43.      }
  44.      // code to execute if condition is not met    
  45. }
  46.        
  47. foreach (DateTime time in times)      
  48. {    
  49.      if (condition)
  50.      {
  51.              // perform action
  52.      }
  53.      else
  54.      {
  55.             // code to execute if condition is not met  
  56.      }
  57. }
  58.        
  59. var times = new List<DateTime>()
  60.     {
  61.         DateTime.Now.AddDays(1), DateTime.Now.AddDays(2), DateTime.Now.AddDays(3), DateTime.Now.AddDays(4)
  62.     };
  63.  
  64. var cutoff = DateTime.Now.AddDays(2);
  65.  
  66. var timesAfterCutoff = times.SkipWhile(datetime => datetime.CompareTo(cutoff) < 1)
  67.     .Select(datetime => datetime);
  68.  
  69. foreach (var dateTime in timesAfterCutoff)
  70. {
  71.     Console.WriteLine(dateTime);
  72. }
  73.  
  74. Console.ReadLine();
  75.        
  76. public static void Main(string[] args)
  77. {
  78.   IEnumerable<DateTime> times = GetTimes();
  79.   foreach (var step in times.StepWise())
  80.   {
  81.     while (condition)
  82.     {
  83.       step.MoveNext();
  84.     }
  85.     Console.WriteLine(step.Current);
  86.   }
  87. }
  88.        
  89. public static class EnumerableExtension
  90. {
  91.     public static IEnumerable<Step<T>> StepWise<T>(this IEnumerable<T> instance)
  92.     {
  93.         using (IEnumerator<T> enumerator = instance.GetEnumerator())
  94.         {
  95.             while (enumerator.MoveNext())
  96.             {
  97.                 yield return new Step<T>(enumerator);
  98.             }
  99.         }
  100.     }
  101.  
  102.     public struct Step<T>
  103.     {
  104.         private IEnumerator<T> enumerator;
  105.  
  106.         public Step(IEnumerator<T> enumerator)
  107.         {
  108.             this.enumerator = enumerator;
  109.         }
  110.  
  111.         public bool MoveNext()
  112.         {
  113.             return enumerator.MoveNext();
  114.         }
  115.  
  116.         public T Current
  117.         {
  118.             get { return enumerator.Current; }
  119.         }
  120.  
  121.     }
  122. }
  123.        
  124. public static IEnumerable<T> FunkyIEnumerable<T>(this Func<Tuple<bool, T>> nextOrNot)
  125.     {
  126.         while(true)
  127.         {
  128.            var result = nextOrNot();
  129.  
  130.             if(result.Item1)
  131.                 yield return result.Item2;
  132.  
  133.             else
  134.                 break;
  135.  
  136.         }
  137.  
  138.         yield break;
  139.  
  140.     }
  141.  
  142.     Func<Tuple<bool, int>> nextNumber = () =>
  143.             Tuple.Create(SomeRemoteService.CanIContinueToSendNumbers(), 1);
  144.  
  145.     foreach(var justGonnaBeOne in nextNumber.FunkyIEnumerable())
  146.             Console.Writeline(justGonnaBeOne.ToString());