Advertisement
damesova

Template Methods

Oct 10th, 2022
773
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.38 KB | None | 0 0
  1. namespace TemplateInterfaceMethods
  2. {
  3.     public class Program
  4.     {
  5.         static void Main(string[] args)
  6.         {
  7.             // Дефиниране на шаблонен метод (типизиран):
  8.             void Swap<M>(ref M param1, ref M param2)
  9.             {
  10.                 M temp;
  11.                 temp = param1;
  12.                 param1 = param2;
  13.                 param2 = temp;
  14.             }
  15.  
  16.             // Извикване на метода с различни типове данни,
  17.             // които стават известни едва след неговата дефиниция
  18.             int x = 5;
  19.             int y = 4;
  20.             Console.WriteLine("Before Swap(): X=" + x + " Y=" + y);
  21.             Swap<int>(ref x, ref y);
  22.             Console.WriteLine("After Swap(): X=" + x + " Y=" + y);
  23.  
  24.  
  25.             string a = "Stilyan";
  26.             string b = "Remzi";
  27.             Console.WriteLine("Before Swap(): A=" + a + " B=" + b);
  28.             Swap<string>(ref a, ref b);
  29.             Console.WriteLine("After Swap(): A=" + a + " B=" + b);
  30.  
  31.             bool bool1 = true;
  32.             bool bool2 = false;
  33.             Console.WriteLine("Before Swap(): bool1=" + bool1 + " bool2=" + bool2);
  34.             Swap<bool>(ref bool1, ref bool2);
  35.             Console.WriteLine("After Swap(): bool1=" + bool1 + " bool2=" + bool2);
  36.         }
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement