You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Extract comments from code samples of "Program structure" article to get them translated (#44422)
* Extract comments from code samples in index page
* Extract comments from code samples of main-command-line page
* Apply suggestions from code review
---------
Co-authored-by: Bill Wagner <[email protected]>
The preceding example uses [*top-level statements*](top-level-statements.md) for the program's entry point. Only one file can have top-level statements. The program's entry point is the first line of program text in that file. You can also create a static method named [`Main`](main-command-line.md) as the program's entry point, as shown in the following example:
15
+
The preceding example uses [*top-level statements*](top-level-statements.md) for the program's entry point. Only one file can have top-level statements. The program's entry point is the first line of program text in that file. In this case, it's the `Console.WriteLine("Hello world!");`.
16
+
You can also create a static method named [`Main`](main-command-line.md) as the program's entry point, as shown in the following example:
Copy file name to clipboardExpand all lines: docs/csharp/fundamentals/program-structure/main-command-line.md
+11-2Lines changed: 11 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,12 +17,14 @@ helpviewer_keywords:
17
17
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.
18
18
19
19
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).
20
+
<br/>Below is the example where the first line executed will display the number of command line arguments:
You can also use Top-level statements in one file as the entry point for your application.
24
25
Just as the `Main` method, top-level statements can also [return values](#main-return-values) and access [command-line arguments](#command-line-arguments).
25
26
For more information, see [Top-level statements](top-level-statements.md).
27
+
<br/>The following example uses a `foreach` loop to display the command line arguments using the `args` variable, and at the end of the program returns a success code (`0`):
Remember to save this program as *MainReturnValTest.cs*.
90
+
87
91
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.
88
92
89
93
You can build the application using the [dotnet CLI](../../../core/tools/dotnet.md)`dotnet build` command.
@@ -122,7 +126,6 @@ class AsyncMainReturnValTest
122
126
123
127
privatestaticasyncTask<int> AsyncConsoleWork()
124
128
{
125
-
// Main body here
126
129
return0;
127
130
}
128
131
}
@@ -132,6 +135,8 @@ This boilerplate code can be replaced by:
In both examples main body of the program is within the body of `AsyncConsoleWork()` method.
139
+
135
140
An advantage of declaring `Main` as `async` is that the compiler always generates the correct code.
136
141
137
142
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,6 +210,10 @@ To compile and run the application from a command prompt, follow these steps:
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/>
214
+
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/>
215
+
After factorial is calculated (stored in `result` variable of type `long`) the verbose result is printed depending on the `result` variable.
216
+
208
217
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.
209
218
210
219
3. Enter the following command to compile the application.
@@ -217,7 +226,7 @@ To compile and run the application from a command prompt, follow these steps:
217
226
218
227
`dotnet run -- 3`
219
228
220
-
5.The command produces this output: `The factorial of 3 is 6.`
229
+
5.If 3 is entered on command line as the program's argument, the output reads: `The factorial of 3 is 6.`
221
230
222
231
> [!NOTE]
223
232
> 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).
0 commit comments