-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
C# Programming guide: Top-level statements #23234
Conversation
helpviewer_keywords: | ||
- "command-line arguments [C#], displaying" | ||
ms.assetid: b8479f2d-9e05-4d38-82da-2e61246e5437 | ||
--- | ||
# How to display command-line arguments (C# Programming Guide) | ||
|
||
Arguments provided to an executable on the command line are accessible through an optional parameter to `Main`. The arguments are provided in the form of an array of strings. Each element of the array contains one argument. White-space between arguments is removed. For example, consider these command-line invocations of a fictitious executable: | ||
Arguments provided to an executable on the command line are accessible in [top-level statements](top-level-statements.md) and through an optional parameter to `Main`. The arguments are provided in the form of an array of strings. Each element of the array contains one argument. White-space between arguments is removed. For example, consider these command-line invocations of a fictitious executable: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is "and" extra?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was intentional, as these are two different options -- top-level statements (implicit Main) and explicit Main. But I'll change "and" to "or" as that may be clearer.
...-guide/main-and-command-args/snippets/top-level-statements-1/top-level-statements (2).csproj
Outdated
Show resolved
Hide resolved
...ming-guide/main-and-command-args/snippets/top-level-statements-3/top-level-statements.csproj
Outdated
Show resolved
Hide resolved
@@ -0,0 +1,90 @@ | |||
--- | |||
title: "Top-level statements - C# Programming Guide" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The feature is also known as "simple programs" if I recall correctly. You may want to include this name for SEO purposes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where did you see this name?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Frequently used in Roslyn repo
For example:
Probably ask Roslyn team for guidance whether this name should be advertised in docs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The public messaging has been consistently to call this feature "top-level statements," and with "simple programs" showing up only in the Roslyn repo, I think using that as an alternate feature name would be unnecessarily confusing, but I'll follow the lead of the top-level statements tutorial and work in the phrase "simple programs" into the text.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Top level statements" is the official name. The product team followed our lead here to avoid the use of "simple". The goal is that programs that use "top level statements" need not be "simple". See FeatherApp for one example of possibilities.
--- | ||
# Top-level statements (C# Programming Guide) | ||
|
||
Starting in C# 9, you don't have to explicitly include a `Main` method in a console application project. Instead, you can use the *top-level statements* feature to minimize the code you have to write. In this case, the compiler provides an implicit class and `Main` method entry point for the application. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I personally lean towards using main without backticks instead of Main
. Because the method the compiler generates isn't called Main
, but <Main>$
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think "implicit" clarifies the status of Main
, but in this instance it will be clearer to repeat "implicit" directly before Main.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think "implicit" resolves this. It could be implicit and called Main
, but the compiler implementation decided it's called <Main>$
.
And actually, from a compiler-point of view, it's not implicit at all (see discussion at dotnet/roslyn#48199), but it's okay to mention implicit for the purpose of this article I think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I mentioned in the other thread, I'll add an explanation that the actual generated name is an implementation detail.
docs/csharp/programming-guide/main-and-command-args/top-level-statements.md
Show resolved
Hide resolved
docs/csharp/programming-guide/main-and-command-args/top-level-statements.md
Show resolved
Hide resolved
@Youssef1313 Thanks for the preliminary review (this is still a draft PR) -- what do you think of my rewrite of the Implicit |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is excellent work @tdykstra
I had a few small comments, and then this is ready to merge.
Thanks again.
@@ -17,7 +17,12 @@ It can also return an `int`: | |||
|
|||
[!code-csharp[csProgGuideMain#13](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideMain/CS/Class3.cs#13)] | |||
|
|||
If the return value from `Main` is not used, returning `void` allows for slightly simpler code. However, returning an integer enables the program to communicate status information to other programs or scripts that invoke the executable file. The return value from `Main` is treated as the exit code for the process. If `void` is returned from `Main`, the exit code will be implicitly `0`. The following example shows how the return value from `Main` can be accessed. | |||
If the return value from `Main` is not used, returning `void` allows for slightly simpler code. However, returning an integer enables the program to communicate status information to other programs or scripts that invoke the executable file. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not related to your PR, but this article should include that Main
can return Task
or Task<int>
.
If we don't add it now, we should make an issue for that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding it now.
@@ -0,0 +1,90 @@ | |||
--- | |||
title: "Top-level statements - C# Programming Guide" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Top level statements" is the official name. The product team followed our lead here to avoid the use of "simple". The goal is that programs that use "top level statements" need not be "simple". See FeatherApp for one example of possibilities.
|
||
:::code language="csharp" source="snippets/top-level-statements-1/Program.cs"::: | ||
|
||
Top-level statements let you write simple programs for small utilities such as Azure Functions and GitHub Actions. They also make it simpler for new C# programmers to get started learning and writing code. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This use of "simple programs" is fine because it does flow naturally with the concept. I like how you've done this Tom.
|
||
> CS7022 The entry point of the program is global code; ignoring 'Main()' entry point. | ||
|
||
In a project with top-level statements, you can't use the [-main](../../language-reference/compiler-options/main-compiler-option.md) compiler option to select the entry point, even if the project has one or more `Main` methods. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note for me, depending on how soon we merge this: I'm update the options section to use the new MSBuild properties rather than the csc
spellings. I'll have to update his depending on the order of merging. See #22797 for the work in progress.
|
||
The compiler generates a method to serve as the program entry point for a project with top-level statements. The name of this method isn't actually `Main`, it's an implementation detail that your code can't reference directly. The signature of the method depends on whether the top-level statements contain the `await` keyword or the `return` statement. The following table shows what the method signature would look like, using the method name `Main` in the table for convenience. | ||
|
||
| Top-level code contains| Implicit `Main` signature | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this table. See my previous comment about the return value of Main
. Adding this table there would be perfect.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding a slightly modified table to both the return value and command-line args docs.
## C# language specification | ||
|
||
[!INCLUDE[CSharplangspec](~/includes/csharplangspec-md.md)] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd add a link to the top level statements speclet. The link would be ~/_csharplang/proposals/csharp-9.0/top-level-statements.md
This note is important, and is added to a file that will be deleted in this PR.
Co-authored-by: Bill Wagner <wiwagn@microsoft.com>
docs/csharp/programming-guide/main-and-command-args/top-level-statements.md
Outdated
Show resolved
Hide resolved
…statements.md Co-authored-by: Youssef Victor <youssefvictor00@gmail.com>
This note is important, and is added to a file that will be deleted in this PR.
This note is important, and is added to a file that will be deleted in this PR.
* Consolidate how to set options Move all information pertaining to how to set compiler options into the index topic for this section. fix markdown lint warning * Consolidate optimizations section remove links to old content * finish the output section. Update listed-by-category.md * finish the input section * first pass at the resources section * Finish the code generation section * Add errors and warning sections. * Finish the language section. * Finish the security section. * Finish Miscellaneous section. * first pass at advanced session is finished 2nd pass through security. * update the miscellaneous section * finish the consolidation of the advanced section * add info on missing options * Finish the grammar check. * clean links in this folder. * Add note from #23234 This note is important, and is added to a file that will be deleted in this PR. * update links to compiler options area Most of these updates were straightforward substitutions of the new option for the old. There are a few exceptions: - Some of the links were from Visual Basic content, but the destination was C# content. Where possible, the link changed to the appropriate VB equivalent. - The displayed hyperlink text was not changed for the F# links. - Some warning and error messages only applied when the command line (older) syntax was used. In those cases, the link was updated, but the displayed text remained the same. - A small number of links were to options no longer supported. These were from error messages for those options. The corresponding supported option was linked in a note about updating. * fix bugs in this section. * edit descriptions and introductions * Final review * Remove extra XML tag. * Apply suggestions from code review Co-authored-by: Tom Dykstra <tdykstra@microsoft.com> * Update docs/csharp/language-reference/compiler-options/advanced.md Co-authored-by: Tom Dykstra <tdykstra@microsoft.com> * Update docs/csharp/language-reference/compiler-options/advanced.md Co-authored-by: Tom Dykstra <tdykstra@microsoft.com> * Update docs/csharp/language-reference/compiler-options/advanced.md Co-authored-by: Tom Dykstra <tdykstra@microsoft.com> * respond to feedback. * markdown lint error. Co-authored-by: Tom Dykstra <tdykstra@microsoft.com>
Fixes #20586