Advertisement
Shokedbrain

lab5 csharp

May 29th, 2022
953
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.92 KB | None | 0 0
  1. using System;
  2. using System.Threading.Tasks;
  3.  
  4. namespace lab5
  5. {
  6.     class Program
  7.     {
  8.         //
  9.         static int Op(int x, int y)
  10.         {
  11.             Console.WriteLine("using Op func");
  12.             return x % y;
  13.         }
  14.    
  15.         // объявляем новый делегат
  16.         public delegate int ModuleDelegate(int x, int y);
  17.  
  18.  
  19.         static int Op2(int x, int y)
  20.         {
  21.             Console.WriteLine("using Op2 func: {0}", x * x + y * y);
  22.             return x * x + y * y;
  23.         }
  24.  
  25.         static async Task Main(string[] args)
  26.         {
  27.             Console.WriteLine("\tpart 1");
  28.             Console.WriteLine("synchronous");  
  29.             // создаём делегат и добавляем в него метод
  30.             ModuleDelegate moduleDelegate = new ModuleDelegate(Op);
  31.             Console.WriteLine("continue working method Main");
  32.             // вычисляем значение, используя делегат
  33.             int res1 = moduleDelegate(7, 5);
  34.             Console.WriteLine("mod res1 {0}", res1);
  35.             Console.WriteLine("asynchronous");
  36.             // не работает в новом .net core
  37.             //IAsyncResult resultObj = moduleDelegate.BeginInvoke(9, 6, null, null);
  38.             //int res2 = moduleDelegate.EndInvoke(resultObj);
  39.             // вместо них используется Task и await
  40.             Console.WriteLine("Starting with Task.Run");
  41.             // создаём новое задание - запуск делегата, передаём параметры
  42.             var task = Task.Run(() => moduleDelegate.Invoke(8,5));
  43.             Console.WriteLine("Waiting on work...");
  44.             // await предваряет выполнение задачи, которая будет выполняться асинхронно
  45.             var asyncRes = await task;
  46.             Console.WriteLine("mod async res {0}", asyncRes);
  47.             Console.WriteLine("\tpart 2");
  48.             // добавляем в наш делегат анонимную функцию
  49.             moduleDelegate += delegate(int x, int y)
  50.             {
  51.                 Console.WriteLine("anonymous delegate using: {0}", x + y);
  52.                 return x + y;
  53.             };
  54.             // добавляем лямбда-функцию
  55.             moduleDelegate += (x, y) =>
  56.             {
  57.                 Console.WriteLine("lambda expression using: {0}", x * y);
  58.                 return x * y;
  59.             };
  60.  
  61.             moduleDelegate += Op2;
  62.             // здесь мы увидим результат работы последнего добавленного метода в делегат
  63.             Console.WriteLine("result of working delegate: {0}", moduleDelegate(17,6));
  64.             Console.WriteLine("\tdeleting one method...");
  65.             moduleDelegate -= Op2;
  66.             Console.WriteLine("result of working delegate after del: {0}", moduleDelegate(17, 6));
  67.         }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement