Skip to content

Commit 18d5200

Browse files
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]>
1 parent 7335196 commit 18d5200

File tree

9 files changed

+16
-21
lines changed

9 files changed

+16
-21
lines changed

docs/csharp/fundamentals/program-structure/index.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@ C# programs consist of one or more files. Each file contains zero or more namesp
1212

1313
:::code language="csharp" source="snippets/toplevel-structure/Program.cs":::
1414

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. 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:
1617

1718
:::code language="csharp" source="snippets/structure/Program.cs":::
1819

20+
In that case the program will start in the first line of `Main` method, which is `Console.WriteLine("Hello world!");`
21+
1922
## Related Sections
2023

2124
You learn about these program elements in the [types](../types/index.md) section of the fundamentals guide:

docs/csharp/fundamentals/program-structure/main-command-line.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ helpviewer_keywords:
1717
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.
1818

1919
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:
2021

2122
:::code language="csharp" source="snippets/main-command-line/TestClass.cs":::
2223

2324
You can also use Top-level statements in one file as the entry point for your application.
2425
Just as the `Main` method, top-level statements can also [return values](#main-return-values) and access [command-line arguments](#command-line-arguments).
2526
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`):
2628

2729
:::code language="csharp" source="snippets/top-level-statements-1/Program.cs":::
2830

@@ -84,6 +86,8 @@ Create a new application by running `dotnet new console`. Modify the `Main` meth
8486

8587
:::code language="csharp" source="snippets/main-command-line/MainReturnValTest.cs":::
8688

89+
Remember to save this program as *MainReturnValTest.cs*.
90+
8791
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.
8892

8993
You can build the application using the [dotnet CLI](../../../core/tools/dotnet.md) `dotnet build` command.
@@ -122,7 +126,6 @@ class AsyncMainReturnValTest
122126

123127
private static async Task<int> AsyncConsoleWork()
124128
{
125-
// Main body here
126129
return 0;
127130
}
128131
}
@@ -132,6 +135,8 @@ This boilerplate code can be replaced by:
132135

133136
:::code language="csharp" source="snippets/main-arguments/Program.cs" id="AsyncMain":::
134137

138+
In both examples main body of the program is within the body of `AsyncConsoleWork()` method.
139+
135140
An advantage of declaring `Main` as `async` is that the compiler always generates the correct code.
136141

137142
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:
205210

206211
:::code language="csharp" source="./snippets/main-command-line/Factorial.cs":::
207212

213+
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+
208217
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.
209218

210219
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:
217226

218227
`dotnet run -- 3`
219228

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.`
221230

222231
> [!NOTE]
223232
> 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).

docs/csharp/fundamentals/program-structure/snippets/main-arguments/Program.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ static async Task<int> Main(string[] args)
1010

1111
private static async Task<int> AsyncConsoleWork()
1212
{
13-
// main body here
1413
return 0;
1514
}
1615
}

docs/csharp/fundamentals/program-structure/snippets/main-command-line/Factorial.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,13 @@ class MainClass
2222
{
2323
static int Main(string[] args)
2424
{
25-
// Test if input arguments were supplied.
2625
if (args.Length == 0)
2726
{
2827
Console.WriteLine("Please enter a numeric argument.");
2928
Console.WriteLine("Usage: Factorial <num>");
3029
return 1;
3130
}
3231

33-
// Try to convert the input arguments to numbers. This will throw
34-
// an exception if the argument is not a number.
35-
// num = int.Parse(args[0]);
3632
int num;
3733
bool test = int.TryParse(args[0], out num);
3834
if (!test)
@@ -42,10 +38,8 @@ static int Main(string[] args)
4238
return 1;
4339
}
4440

45-
// Calculate factorial.
4641
long result = Functions.Factorial(num);
4742

48-
// Print result.
4943
if (result == -1)
5044
Console.WriteLine("Input must be >= 0 and <= 20.");
5145
else
@@ -54,6 +48,3 @@ static int Main(string[] args)
5448
return 0;
5549
}
5650
}
57-
// If 3 is entered on command line, the
58-
// output reads: The factorial of 3 is 6.
59-

docs/csharp/fundamentals/program-structure/snippets/main-command-line/MainReturnValTest.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// Save this program as MainReturnValTest.cs.
21
class MainReturnValTest
32
{
43
static int Main()

docs/csharp/fundamentals/program-structure/snippets/main-command-line/TestClass.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
{
33
static void Main(string[] args)
44
{
5-
// Display the number of command line arguments.
65
Console.WriteLine(args.Length);
76
}
87
}

docs/csharp/fundamentals/program-structure/snippets/structure/Program.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ class Program
3131
{
3232
static void Main(string[] args)
3333
{
34-
//Your program starts here...
3534
Console.WriteLine("Hello world!");
3635
}
3736
}

docs/csharp/fundamentals/program-structure/snippets/top-level-statements-1/Program.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@
33
StringBuilder builder = new();
44
builder.AppendLine("The following arguments are passed:");
55

6-
// Display the command line arguments using the args variable.
76
foreach (var arg in args)
87
{
98
builder.AppendLine($"Argument={arg}");
109
}
1110

1211
Console.WriteLine(builder.ToString());
1312

14-
// Return a success code.
1513
return 0;

docs/csharp/fundamentals/program-structure/snippets/toplevel-structure/Program.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
// A skeleton of a C# program
2-
using System;
1+
using System;
32

4-
// Your program starts here:
53
Console.WriteLine("Hello world!");
64

75
namespace YourNamespace

0 commit comments

Comments
 (0)