HomeCSharpC# Error CS4009 – A void or int returning entry point cannot be async

C# Error CS4009 – A void or int returning entry point cannot be async

C# Error

CS4009 – A void or int returning entry point cannot be async

Reason for the Error & Solution

‘Type.Method’: an entry point cannot be marked with the async modifier.

You cannot use the async keyword in the application entry point (typically the Main method).

Important

Starting with C# 7.1, the Main method can have an async modifier. For more information, see . For information about how to select the C# language version, see the article.

Example

The following example produces CS4009:

using System;
using System.Threading.Tasks;

public class Example
{
    public static async void Main()
    {
        Console.WriteLine("About to wait two seconds");
        await WaitTwoSeconds();
        Console.WriteLine("About to exit the program");
    }

    private static async Task WaitTwoSeconds()
    {
        await Task.Delay(2000);
        Console.WriteLine("Returning from an asynchronous method");
    }
}

To correct this error

used by the project to 7.1 or higher.

If you use C# 7.0 or lower, remove the async keyword from the signature of the application entry point. Also remove any await keywords you’ve used to await asynchronous methods in your application entry point.

However, you still have to wait for the asynchronous method to complete before your entry point resumes execution. Otherwise, compilation generates compiler warning CS4014, and the application will terminate before the asynchronous operation completes. The following example illustrates this problem:

using System;
using System.Threading.Tasks;

public class Example
{
   public static void Main()
   {
       Console.WriteLine("About to wait two seconds");
       WaitTwoSeconds();
       Console.WriteLine("About to exit the program");
   }

   private static async Task WaitTwoSeconds()
   {
      await Task.Delay(2000);
      Console.WriteLine("Returning from an asynchronous method");
   }
}
// The example displays the following output:
//       About to wait two seconds
//       About to exit the program

To await a method that returns a , call its method, as the following example illustrates:

using System;
using System.Threading.Tasks;

public class Example
{
   public static void Main()
   {
       Console.WriteLine("About to wait two seconds");
       WaitTwoSeconds().Wait();
       Console.WriteLine("About to exit the program");
   }

   private static async Task WaitTwoSeconds()
   {
      await Task.Delay(2000);
      Console.WriteLine("Returning from an asynchronous method");
   }
}
// The example displays the following output:
//       About to wait two seconds
//       Returning from an asynchronous method
//       About to exit the program

To await a method that returns a , retrieve the value of its property, as the following example does:

using System;
using System.Threading.Tasks;

public class Example
{
   public static void Main()
   {
       Console.WriteLine("About to wait two seconds");
       int value = WaitTwoSeconds().Result;
       Console.WriteLine($"Value returned from the aynnc operation: {value}");
       Console.WriteLine("About to exit the program");
   }

   private static async Task<int> WaitTwoSeconds()
   {
      await Task.Delay(2000);
      Console.WriteLine("Returning from an asynchronous method");
      return 100;
   }
}
// The example displays the following output:
//       About to wait two seconds
//       Returning from an asynchronous method
//       Value returned from the aynnc operation: 100
//       About to exit the program

Leave a Reply

You May Also Like

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...