Advertisement
GeneralGDA

CS code

Mar 8th, 2019
463
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.95 KB | None | 0 0
  1. namespace ConsoleAppSandbox
  2. {
  3.     static class Program
  4.     {
  5.         private static void Display<T>(System.Collections.Generic.IEnumerable<T> values)
  6.         {
  7.             using (var e = values.GetEnumerator())
  8.             {
  9.                 int counter = 11;
  10.                 while (true)
  11.                 {
  12.                     if (false == e.MoveNext())
  13.                     {
  14.                         break;
  15.                     }
  16.  
  17.                     System.Console.Write(e.Current?.ToString());
  18.                     counter -= 1;
  19.  
  20.                     if (0 == counter)
  21.                     {
  22.                         break;
  23.                     }
  24.                 }
  25.             }
  26.         }
  27.  
  28.         sealed class Detector
  29.         {
  30.             internal int Counter { get; private set; }
  31.  
  32.             internal int Overflow { get; private set; }
  33.  
  34.             public System.Collections.Generic.IEnumerable<double> Do()
  35.             {
  36.                 try
  37.                 {
  38.                     for (int i = 0; i < 10; i++)
  39.                     {
  40.                         yield return 0;
  41.                     }
  42.  
  43.                     ++Overflow;
  44.                     yield return 0;
  45.                 }
  46.                 finally
  47.                 {
  48.                     Counter += 1;
  49.                 }
  50.             }
  51.         }
  52.  
  53.         public static void Main(string[] args)
  54.         {
  55.             Display(args);
  56.  
  57.             System.Console.SetOut(new System.IO.StreamWriter(System.IO.Stream.Null));
  58.  
  59.             try
  60.             {
  61.                 Display(new string[] {null, null});
  62.             }
  63.             catch (System.Exception)
  64.             {
  65.                 throw new System.Exception("В C#, пока, нет способов на этапе компиляции гарантировать, что последовательность не будет содержать нулевые ссылки. Надо бы их корректно обрабатывать.");
  66.             }
  67.  
  68.             Detector foo = new Detector();
  69.             Display(foo.Do());
  70.  
  71.             if (foo.Counter != 1)
  72.             {
  73.                 throw new System.Exception("IEnumerator может реализовывать интерфейс IDisposable!");
  74.             }
  75.  
  76.             if (foo.Overflow != 0)
  77.             {
  78.                 throw new System.Exception("Код не должен перебирать более 10 элементов последовательности.");
  79.             }
  80.         }
  81.  
  82.         static void Main1()
  83.         {
  84.             const string fileName = "tmp.tmp";
  85.  
  86.             byte[] dataToWrite = new byte[] { 0, 1, 0, 1 };
  87.  
  88.             System.IO.FileStream file = new System.IO.FileStream(fileName, System.IO.FileMode.Create);
  89.  
  90.             file.Write(dataToWrite, 0, dataToWrite.Length);
  91.  
  92.             System.GC.Collect();
  93.             System.GC.WaitForPendingFinalizers();
  94.  
  95.             System.IO.File.Delete(fileName);
  96.         }
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement