-
Notifications
You must be signed in to change notification settings - Fork 6k
Extract comments from code samples of "Program structure" article to get them translated #44422
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
Changes from 2 commits
b528cca
1bf3a50
950d0a8
24f02b7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,12 +17,14 @@ | |
The `Main` method is the entry point of a C# application. When the application is started, the `Main` method is the first method that is invoked. | ||
|
||
There can only be one entry point in a C# program. If you have more than one class that has a `Main` method, you must compile your program with the **StartupObject** compiler option to specify which `Main` method to use as the entry point. For more information, see [**StartupObject** (C# Compiler Options)](../../language-reference/compiler-options/advanced.md#mainentrypoint-or-startupobject). | ||
<br/>Below is the example which first line executed will display the number of command line arguments: | ||
|
||
:::code language="csharp" source="snippets/main-command-line/TestClass.cs"::: | ||
|
||
You can also use Top-level statements in one file as the entry point for your application. | ||
Just as the `Main` method, top-level statements can also [return values](#main-return-values) and access [command-line arguments](#command-line-arguments). | ||
For more information, see [Top-level statements](top-level-statements.md). | ||
<br/>The following example uses `foreach` loop to display the command line arguments using the args variable, and at the end of the program returns a success code (`0`): | ||
BillWagner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
:::code language="csharp" source="snippets/top-level-statements-1/Program.cs"::: | ||
|
||
|
@@ -84,6 +86,8 @@ | |
|
||
:::code language="csharp" source="snippets/main-command-line/MainReturnValTest.cs"::: | ||
|
||
Remember to save this program as *MainReturnValTest.cs*. | ||
|
||
When a program is executed in Windows, any value returned from the `Main` function is stored in an environment variable. This environment variable can be retrieved using `ERRORLEVEL` from a batch file, or `$LastExitCode` from PowerShell. | ||
|
||
You can build the application using the [dotnet CLI](../../../core/tools/dotnet.md) `dotnet build` command. | ||
|
@@ -122,7 +126,6 @@ | |
|
||
private static async Task<int> AsyncConsoleWork() | ||
{ | ||
// Main body here | ||
return 0; | ||
} | ||
} | ||
|
@@ -132,6 +135,8 @@ | |
|
||
:::code language="csharp" source="snippets/main-arguments/Program.cs" id="AsyncMain"::: | ||
|
||
In both examples main body of the program is within the body of `AsyncConsoleWork()` method. | ||
|
||
An advantage of declaring `Main` as `async` is that the compiler always generates the correct code. | ||
|
||
When the application entry point returns a `Task` or `Task<int>`, the compiler generates a new entry point that calls the entry point method declared in the application code. Assuming that this entry point is called `$GeneratedMain`, the compiler generates the following code for these entry points: | ||
|
@@ -205,19 +210,23 @@ | |
|
||
:::code language="csharp" source="./snippets/main-command-line/Factorial.cs"::: | ||
|
||
At the beginning of the `Main` method the program tests if input arguments were not supplied comparing length of `args` argument to `0` and displays the help if no argument are found. | ||
<br/>If arguments are provided (`args.Length` is greater than 0) program tries to convert the input arguments to numbers. This will throw an exception if the argument is not a number. | ||
<br/>After factorial is calculated (stored in `result` variable of type `long`) the verbose result is printed depending on the `result` variable. | ||
BillWagner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
2. From the **Start** screen or **Start** menu, open a Visual Studio **Developer Command Prompt** window, and then navigate to the folder that contains the file that you created. | ||
Check failure on line 217 in docs/csharp/fundamentals/program-structure/main-command-line.md
|
||
|
||
3. Enter the following command to compile the application. | ||
Check failure on line 219 in docs/csharp/fundamentals/program-structure/main-command-line.md
|
||
|
||
`dotnet build` | ||
|
||
If your application has no compilation errors, an executable file that's named *Factorial.exe* is created. | ||
|
||
4. Enter the following command to calculate the factorial of 3: | ||
Check failure on line 225 in docs/csharp/fundamentals/program-structure/main-command-line.md
|
||
|
||
`dotnet run -- 3` | ||
|
||
5. The command produces this output: `The factorial of 3 is 6.` | ||
5. If 3 is entered on command line as the program's argument, the output reads: `The factorial of 3 is 6.` | ||
Check failure on line 229 in docs/csharp/fundamentals/program-structure/main-command-line.md
|
||
|
||
> [!NOTE] | ||
> When running an application in Visual Studio, you can specify command-line arguments in the [Debug Page, Project Designer](/visualstudio/ide/reference/debug-page-project-designer). | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
// Save this program as MainReturnValTest.cs. | ||
class MainReturnValTest | ||
{ | ||
static int Main() | ||
|
Uh oh!
There was an error while loading. Please reload this page.