Advertisement
Guest User

Delegate

a guest
Apr 11th, 2014
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.00 KB | None | 0 0
  1. class Array
  2. {
  3.     public delegate void Transformer(ref int item);
  4.  
  5.     private int[] array;
  6.  
  7.     public Array(int length)
  8.     {
  9.         Length = length;
  10.         array = new int[Length];
  11.     }
  12.  
  13.     public int Length { get; set; }
  14.  
  15.     public int this[int idx]
  16.     {
  17.         get { return array[idx]; }
  18.         set { array[idx] = value; }
  19.     }
  20.  
  21.     public void Transform(Transformer t)
  22.     {
  23.         for (int i = 0; i < array.Length; ++i)
  24.         {
  25.             t(ref array[i]);
  26.         }
  27.     }
  28. }
  29.  
  30. class Program
  31. {
  32.     static public void TransformerMethod(ref int item)
  33.     {
  34.         item *= item;
  35.     }
  36.  
  37.     static void Main(string[] args)
  38.     {
  39.         Array array = new Array(10);
  40.  
  41.         for (int i = 0; i < array.Length; ++i)
  42.         {
  43.             array[i] = i;
  44.         }
  45.  
  46.         array.Transform(Program.TransformerMethod);
  47.  
  48.         for (int i = 0; i < array.Length; ++i)
  49.         {
  50.             Console.WriteLine(array[i]);
  51.         }
  52.  
  53.         Console.ReadKey();
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement