Advertisement
myname0

delegate

Apr 22nd, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.14 KB | None | 0 0
  1.  namespace @delegate
  2. {  
  3.     class TestArray <T>
  4.     {
  5.         private T[] array;
  6.  
  7.         public TestArray( T [] _array)
  8.         {
  9.             //array = new T[_array.Length];
  10.             //for (int i = 0; i < _array.Length; i++)
  11.             //{
  12.             //    array[i] = _array[i];
  13.             //}
  14.             array = _array;
  15.         }
  16.  
  17.         public delegate int Compare( T first, T second);
  18.  
  19.         public void Compares(Compare Comp)
  20.         {
  21.             T t;
  22.             for (int i = 0; i < array.Length; ++i)
  23.                 for(int j = 0; j < array.Length - i -1; j ++)
  24.                 {
  25.                     if(Comp(array[j], array[j+1]) == 1)
  26.                     {              
  27.                         t = array[j];
  28.                         array[j] = array[j + 1];
  29.                         array[j+1] = t;
  30.                     }
  31.                 }
  32.         }
  33.  
  34.         public delegate void EventHandler();
  35.         public event EventHandler message;
  36.  
  37.         //private void OnAnotherDecade(int decade)
  38.         //{
  39.         //    if (AnotherDecade != null)
  40.         //    {
  41.         //        AnotherDecade(this, new TestDecadeEventArgs(decade));
  42.         //    }
  43.         //}
  44.  
  45.         public void Sort(Compare comp)
  46.         {
  47.             //int decimalCounter = 0;
  48.             Compares(comp);
  49.             message();          
  50.             //for (int i = 0; i < array.Length; i++)
  51.             //{
  52.             //    Console.WriteLine(array[i]);
  53.             //    decimalCounter++;
  54.             //    if (decimalCounter >= 10)
  55.             //    {
  56.             //        decimalCounter = 0;
  57.             //        OnAnotherDecade(i / 10);
  58.             //    }
  59.             //    Thread.Sleep(TimeSpan.FromMilliseconds(300));
  60.             //}
  61.         }
  62.  
  63.         public void Print()
  64.         {
  65.             foreach(T i in array)
  66.                 Console.WriteLine("{0}", i);
  67.         }
  68. }
  69.  
  70.     //public class TestDecadeEventArgs : EventArgs
  71.     //    {
  72.     //        public int Decade { get; private set; }
  73.     //        public TestDecadeEventArgs(int decade)
  74.     //        {
  75.     //            Decade = decade;
  76.     //        }
  77.     //    }
  78.  
  79.     class Programm
  80.     {
  81.  
  82.         private static void Message()
  83.         {
  84.             Console.WriteLine("Array is sorted");
  85.         }
  86.  
  87.         public static int CompareString(string x, string y)
  88.         {
  89.             if (x.Length < y.Length) return -1;
  90.             else if (x.Length > y.Length) return 1;
  91.             else return string.Compare(x, y);
  92.         }
  93.  
  94.         public static int CompareInt(int x, int y)
  95.         {
  96.             if (x > y) return 1;
  97.             else if (x == y) return 0;
  98.             else return -1;
  99.         }
  100.  
  101.         public static int CompareDouble(double x, double y)
  102.         {
  103.             if (x > y) return 1;
  104.             else if (x == y) return 0;
  105.             else return -1;
  106.         }
  107.  
  108.        
  109.         static void Main(string[] args)
  110.         {
  111.             string[] str1 = { "as", "absd", "hello", "say", "abbb", "conclusion", "cat", "story", "factory", "advance", "cold", "ice", "touch" };
  112.             string[] str2 = { "cover", "mussle", "boullet", "house", "horse", "statisfaction", "moon", "black", "night", "middle", "contry" };
  113.             int[] int1 = { 12, 13, 21, 32, -2, 5, 19, 40 };
  114.             double[] doub1 = { 123.4, 12, 432, 65, 213.33, 232.76, 654.9 };
  115.  
  116.             TestArray<string> arr1 = new TestArray<string>(str1);
  117.             TestArray<int> arr3 = new TestArray<int>(int1);
  118.             TestArray<string> arr4 = new TestArray<string>(str2);
  119.  
  120.             arr1.message += Message;
  121.             arr1.Sort(CompareString);
  122.             arr1.Print();
  123.             Console.WriteLine();
  124.  
  125.             arr3.message += Message;
  126.             arr3.Sort(CompareInt);
  127.             arr3.Print();
  128.             Console.WriteLine();
  129.  
  130.             arr4.message += Message;
  131.             arr4.Sort(CompareString);
  132.             arr4.Print();
  133.  
  134.             //Thread th1 = new Thread(arr1.Run(CompareString));
  135.             //th1.Start(CompareString);
  136.             //Thread th2 = new Thread(arr4.Run);
  137.             //th2.Start(CompareString);
  138.         }
  139.     }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement