Advertisement
mrAnderson33

Untitled

Jan 12th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.86 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.IO;
  4. using System.Runtime.Serialization.Formatters.Binary;
  5. using System.Threading;
  6. using System.Windows.Forms;
  7.  
  8. namespace Traning
  9. {
  10.     [Serializable]
  11.     class MyArr : IEnumerable
  12.     {
  13.         private int[] arr;
  14.  
  15.         public int Size{ get => arr.Length;}
  16.  
  17.         public MyArr(int size = 0) { arr = new int[size]; }
  18.         public MyArr(int[] _arr) => arr = _arr;
  19.  
  20.         public MyArr FullRandomNumres()
  21.         {
  22.             var rnd = new Random();
  23.             for (int i = 0; i < arr.Length; ++i) arr[i] = rnd.Next(-100, 100);
  24.             return this;
  25.         }
  26.  
  27.         public MyArr Print()
  28.         {
  29.             foreach (var n in arr) Console.Write($"{n}  ");
  30.             return this;
  31.         }
  32.  
  33.         public static void SaveToFile(string filename, MyArr value)
  34.         {
  35.             using (var fs = new FileStream(filename, FileMode.OpenOrCreate))
  36.             {
  37.                 new BinaryFormatter().Serialize(fs,value);
  38.             }
  39.         }
  40.  
  41.         public static MyArr LoadFromFile(string filename)
  42.         {
  43.             using (var fs = File.OpenRead(filename))
  44.             {
  45.                 return new MyArr( new BinaryFormatter().Deserialize(fs) as int []);
  46.             }
  47.         }
  48.        
  49.         public int this[int index] { get => arr[index]; set => arr[index] = value;}
  50.  
  51.         public IEnumerator GetEnumerator() => arr.GetEnumerator();
  52.     }
  53.  
  54.     public delegate int DisplayHandler();
  55.     public delegate DialogResult show(string x);
  56.  
  57.     class Program
  58.     {
  59.        static void Main(string[] args)
  60.         {
  61.             /*var arr = new MyArr(10);
  62.             arr.FullRandomNumres().Print();
  63.             Console.WriteLine();
  64.  
  65.             var filename = @"c:\input";
  66.  
  67.             MyArr.SaveToFile(filename, arr);
  68.             arr = MyArr.LoadFromFile(filename);
  69.             arr.Print();*/
  70.             DisplayHandler handler = new DisplayHandler(Display);
  71.             var s = new show((x) => MessageBox.Show(x));
  72.  
  73.             IAsyncResult resultObj = handler.BeginInvoke(null, null);
  74.             var rs = s.BeginInvoke("Hello",null, null);
  75.        
  76.             Thread.Sleep(1000);
  77.  
  78.             Console.WriteLine("Продолжается работа метода Main");
  79.             int result = handler.EndInvoke(resultObj);
  80.  
  81.             Console.WriteLine("Результат равен {0}", result);
  82.  
  83.         }
  84.         static int Display()
  85.         {
  86.             Console.WriteLine("Начинается работа метода Display...");
  87.  
  88.             int result = 0;
  89.             for (int i = 1; i < 10; i++)
  90.             {
  91.                 result += i * i;
  92.             }
  93.             Thread.Sleep(3000);
  94.             Console.WriteLine("Завершается работа метода Display....");
  95.             return result;
  96.         }
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement