docs
docs copied to clipboard
The "Main()" documentation is misleading about async
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
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.