docs icon indicating copy to clipboard operation
docs copied to clipboard

The "Main()" documentation is misleading about async

Open vernou opened this issue 9 months ago • 1 comments

Type of issue

Other (describe below)

Description

In the documentation Main() and command-line arguments, I found the part about async misleading.

For example this part :

The following list shows valid Main signatures:

public static void Main() { }
public static int Main() { }
public static void Main(string[] args) { }
public static int Main(string[] args) { }
public static async Task Main() { }
public static async Task<int> Main() { }
public static async Task Main(string[] args) { }
public static async Task<int> Main(string[] args) { }

Like explained by :

If and only if Main returns a Task or Task, the declaration of Main may include the async modifier. This specifically excludes an async void Main method.

This other signatures is also valid :

public static Task Main() { }
public static Task<int> Main() { }
public static Task Main(string[] args) { }
public static Task<int> Main(string[] args) { }

Also, the return type of a method is not part of the signature of the method. I think the term declaration is better in this context.


Moreover, the boilerplate code is :

class AsyncMainReturnValTest
{
    public static void Main()
    {
        AsyncConsoleWork().GetAwaiter().GetResult();
    }

    private static async Task<int> AsyncConsoleWork()
    {
        // Main body here
        return 0;
    }
}

The result need to be returned, so this boilerplate code should be :

class AsyncMainReturnValTest
{
    public static void Main()
    {
        return AsyncConsoleWork().GetAwaiter().GetResult();
    }

    private static async Task<int> AsyncConsoleWork()
    {
        // Main body here
        return 0;
    }
}

If you agree, I can do a PR to improve this page.

Page URL

https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/program-structure/main-command-line

Content source URL

https://github.com/dotnet/docs/blob/main/docs/csharp/fundamentals/program-structure/main-command-line.md

Document Version Independent Id

d942d65a-87e4-1525-bd94-b005f1b7cbac

Article author

@BillWagner

Metadata

  • ID: 06ef4bd7-7012-8afd-721a-a4c31db20202
  • Service: dotnet-csharp
  • Sub-service: fundamentals

vernou avatar May 16 '24 19:05 vernou