Guest User

Untitled

a guest
Feb 21st, 2015
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.17 KB | None | 0 0
  1.  
  2. // Простой пример применения делегата.
  3. using System;
  4.  
  5. // Объявить тип делегата.
  6. delegate string StrMod(string str) ;
  7.  
  8. class StrMod
  9. {
  10.     // Заменить пробелы дефисами.
  11.     static string ReplaceSpaces(string s) {
  12.         // ...
  13.     }
  14.  
  15.     // Удалить пробелы.
  16.     static string RemoveSpaces(string s) {
  17.         // ...
  18.     }
  19.  
  20.     // Обратить-строку.
  21.     static string Reverse(string s) {
  22.         // ...
  23.     }
  24. }
  25.  
  26. static void Main() {
  27.  
  28.     // Сконструировать делегат.
  29.     StrMod example = new StrMod(ReplaceSpaces);
  30.    
  31.     string str;
  32.  
  33.     // Вызвать методы с помощью делегата.
  34.     str = example("Саша отсосала у Алёши"); Console.WriteLine(str);
  35.  
  36.     example = new StrMod(RemoveSpaces) ;
  37.     str = example("Саша отсосала у Алёши"); Console.WriteLine(str);
  38.  
  39.     example = new StrMod(Reverse);
  40.     str = example("Саша отсосала у Алёши"); Console.WriteLine(str);
  41.     }
  42. } // class
  43.  
  44. /*
  45. Замена пробелов дефисами.
  46. Удаление пробелов.
  47. Обращение строки.
  48. */
Advertisement
Add Comment
Please, Sign In to add comment