C#. Making Func delegates async

ex.1

        static void Main(string[] args)
        {
            SmthAsync();
            for (int i = 0; i < 4; i++)
            {
                Thread.Sleep(1000);
                Console.WriteLine(i);
            }
        }

        private static Func<int, Task> delaySmth = async (sec) =>
        {
            await Task.Delay(sec * 1000);
            Console.WriteLine("passed " + sec.ToString());
        };

        static async void SmthAsync()
        {
            await delaySmth(4);
        }

ex.2

        static void Main(string[] args)
        {
            SmthAsync();
            for (int i = 0; i < 4; i++)
            {
                Thread.Sleep(1000);
                Console.WriteLine(i);
            }
        }


        static Func<int, int, Task<int>> delayedSum = async (a, b) =>
        {
            await Task.Delay(2000);
            return a + b;
        };

        static async void SmthAsync()
        {
            int sum = await delayedSum(998, 1);
            Console.WriteLine(sum);
        }

ex. 2

This entry was posted in Без рубрики. Bookmark the permalink.

Leave a Reply

Your email address will not be published.