Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The "Main()" documentation is misleading about async #40972

Open
vernou opened this issue May 16, 2024 · 1 comment
Open

The "Main()" documentation is misleading about async #40972

vernou opened this issue May 16, 2024 · 1 comment
Assignees
Labels
doc-enhancement Improve the current content [org][type][category] dotnet-csharp/svc fundamentals/subsvc Pri1 High priority, do before Pri2 and Pri3

Comments

@vernou
Copy link
Contributor

vernou commented May 16, 2024

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
@dotnet-bot dotnet-bot added the ⌚ Not Triaged Not triaged label May 16, 2024
@issues-automation issues-automation bot added dotnet-csharp/svc fundamentals/subsvc Pri1 High priority, do before Pri2 and Pri3 labels May 16, 2024
@BillWagner
Copy link
Member

Hi @vernou I like some of these ideas.

This other signatures is also valid :

Yes, the async modifier is optional. Let's make this change. However, it's important to keep the restriction that async void isn't valid for Main.

I think the term declaration is better in this context.

Yes. "declaration" should be used instead of "signature" throughout this article.

Moreover, the boilerplate code is :

There are two examples here. I'd prefer changing the first declaration of AsyncConsoleWork to return a Task instead of a Task<int>. The two samples should show the difference between returning Task and Task<int.

Thanks for volunteering to address these. I'll watch for a PR.

@BillWagner BillWagner added the doc-enhancement Improve the current content [org][type][category] label May 17, 2024
@dotnet-bot dotnet-bot removed the ⌚ Not Triaged Not triaged label May 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc-enhancement Improve the current content [org][type][category] dotnet-csharp/svc fundamentals/subsvc Pri1 High priority, do before Pri2 and Pri3
Projects
None yet
Development

No branches or pull requests

3 participants