Veikedo

[C# interview] async

Dec 7th, 2020
477
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.38 KB | None | 0 0
  1. using System;
  2. using System.Threading.Tasks;
  3.  
  4.  
  5. // what will be the output
  6. public class MyClass
  7. {
  8.     private static int _count;
  9.     public static void Work()
  10.     {
  11.         for (var i = 0; i < 10; i++)
  12.         {
  13.             _count++;
  14.         }
  15.     }
  16.  
  17.     public static async Task Main()
  18.     {
  19.         var t1 = Task.Run(Work);
  20.         var t2 = Task.Run(Work);
  21.         await Task.WhenAll(t1, t2);
  22.         Console.WriteLine(_count);
  23.     }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment