I need a very simple async await console example RRS feed

  • Question

  • Hi   i am new to asynchronous programming. I tried many tutorials but still not clear about how it works. So I need very simple to the point tutorial that uses console app. 
    Thursday, July 11, 2019 1:56 PM

All replies

  • async Main method has been supported since C# 7.1

    Here is a quick example. Its a .NET 7.2 Console app.

    using System;
    using System.Threading.Tasks;
    namespace ConsoleApp1
        class Program
            static async Task Main(string[] args)
                var number = 6;
                var factorial = await FactorialAsync(number);
                Console.WriteLine($"Factorial of {number}: {factorial}");
                Console.WriteLine("Enter any key to exit.");
                Console.ReadLine(); } private static Task<int> FactorialAsync(int n) { return Task.Run(() => Calc(n)); int Calc(int i) //Local function { if (i == 1) { return 1; } return i * Calc(i - 1); } } } }

    william xifaras

    Thursday, July 11, 2019 2:16 PM
  • I copied your code but I am getting :

    Severity Code Description Project File Line Suppression State
    Error CS5001 Program does not contain a static 'Main' method suitable for an entry point

    I am using .Net 4.7.2. 

    Thursday, July 11, 2019 4:48 PM
  • Hi kobosh3,

    Thank you for posting here.

    You can refer the following reference about the Error:

    async at console app in C#?

    Besides, the following document can help you understand asynchronous programming in C#:

    The Task asynchronous programming model in C#

    Best Regards,

    Xingyu Zhao

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, July 12, 2019 5:05 AM
  • Friday, July 12, 2019 5:29 PM