Skip to content

Commit 87b84eb

Browse files
BillWagneradegeo
andauthored
Apply suggestions from code review
Co-authored-by: Andy (Steve) De George <[email protected]>
1 parent 079b350 commit 87b84eb

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

docs/csharp/fundamentals/tutorials/file-based-programs.md

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@ ai-usage: ai-assisted
1010
# Tutorial: Build file-based C# programs
1111

1212
> [!IMPORTANT]
13-
>
1413
> File-based programs are a feature of .NET 10, which is in preview.
1514
> Some information relates to prerelease product that might be modified before release. Microsoft makes no warranties, express or implied, with respect to the information provided here.
1615
17-
*File-based programs* are programs contained within a single `*.cs` file that are built and run without a corresponding project (`*.csproj`) file. File-based programs are ideal for learning C# because they have less complexity: The entire program is stored in a single file. File-based programs are also useful for building command line utilities. On Unix platforms, file-based programs can be executed using `#!` (shebang) directives.
16+
*File-based programs* are programs contained within a single `*.cs` file that are built and run without a corresponding project (`*.csproj`) file. File-based programs are ideal for learning C# because they have less complexity: The entire program is stored in a single file. File-based programs are also useful for building command line utilities. On Unix platforms, file-based programs can be run using `#!` (shebang) directives.
1817

1918
In this tutorial, you:
2019

@@ -45,9 +44,9 @@ Save the file. Then, open the integrated terminal in Visual Studio Code and type
4544
dotnet run AsciiArt.cs
4645
```
4746

48-
The first time you run this program, the `dotnet` host builds the executable from your source file, stores build artifacts in a temporary folder, then runs the created executable. You can verify this experience by typing `dotnet run AsciiArt.cs` again. This time, the `dotnet` host determines that the executable is current, and runs the executable without building it again. You don't see any build output. The executable runs without the extra build step.
47+
The first time you run this program, the `dotnet` host builds the executable from your source file, stores build artifacts in a temporary folder, then runs the created executable. You can verify this experience by typing `dotnet run AsciiArt.cs` again. This time, the `dotnet` host determines that the executable is current, and runs the executable without building it again. You don't see any build output.
4948

50-
The preceding steps demonstrate that file based programs aren't script files. They're C# source files that are built using a generated project file in a temporary folder. One of the lines of output when you built the program should look something like this (on Windows):
49+
The preceding steps demonstrate that file based programs aren't script files. They're C# source files that are built using a generated project file in a temporary folder. One of the lines of output displayed when you built the program should look something like this (on Windows):
5150

5251
```dotnetcli
5352
AsciiArt succeeded (7.3s) → AppData\Local\Temp\dotnet\runfile\AsciiArt-85c58ae0cd68371711f06f297fa0d7891d0de82afde04d8c64d5f910ddc04ddc\bin\debug\AsciiArt.dll
@@ -85,9 +84,9 @@ The `--` option indicates that all following command arguments should be passed
8584

8685
This version demonstrates these new concepts:
8786

88-
1. The command line arguments are passed to the program using the predefined variable `args`. The `args` variable is an array of strings: `string[]`. If the length of `args` is 0, that means no arguments were provided. Otherwise, each word on the argument list is stored in the corresponding entry in the array.
89-
1. The `string.Join` method joins multiple strings into a single string, with the specified separator. In this case, the separator is a single space.
90-
1. `Console.WriteLine` writes the string to the standard output console, followed by a new line.
87+
- The command line arguments are passed to the program using the predefined variable `args`. The `args` variable is an array of strings: `string[]`. If the length of `args` is 0, that means no arguments were provided. Otherwise, each word on the argument list is stored in the corresponding entry in the array.
88+
- The `string.Join` method joins multiple strings into a single string, with the specified separator. In this case, the separator is a single space.
89+
- `Console.WriteLine` writes the string to the standard output console, followed by a new line.
9190

9291
That handles command line arguments correctly. Now, add the code to handle reading input from standard input (`stdin`) instead of command line arguments. Add the following `else` clause to the `if` statement you added in the preceding code:
9392

@@ -156,13 +155,13 @@ Next, add a package that supports ASCII art, [Colorful.Console](https://www.nuge
156155

157156
> [!IMPORTANT]
158157
>
159-
> The version `1.2.15` was the latest version when this tutorial was last updated. If there's a newer version available, use the latest version to ensure you have the latest security packages.
158+
> The version `1.2.15` was the latest version of the `Colorful.Console` package when this tutorial was last updated. Check the package's [NuGet page](https://www.nuget.org/packages/Colorful.Console) for the latest version to ensure you use a package version with the latest security fixes.
160159
161160
Next, change the lines that call `Console.WriteLine` to use the `Colorful.Console.WriteAscii` method instead:
162161

163162
:::code language="csharp" source="./snippets/file-based-programs/AsciiArt.cs" id="WriteAscii":::
164163

165-
Run the program, and you see ASCII art output instead of echoed text. Next, let's add command line parsing. The current version writes each word as a different line of output. The command line arguments you add support two features:
164+
Run the program, and you see ASCII art output instead of echoed text. Next, let's add command line parsing. The current version writes each word as a different line of output. The command line arguments you added support two features:
166165

167166
1. Quote multiple words that should be written on one line:
168167

@@ -176,16 +175,16 @@ Run the program, and you see ASCII art output instead of echoed text. Next, let'
176175
AsciiArt.cs --delay 1000
177176
```
178177

179-
Users should be able to use both together.
178+
Users should be able to use both arguments together.
180179

181180
Most command line applications need to parse command line arguments to handle options, commands, and user input effectively. The [`System.CommandLine` library](../../../standard/commandline/index.md) provides comprehensive capabilities to handle commands, subcommands, options, and arguments, allowing you to concentrate on what your application does rather than the mechanics of parsing command line input.
182181

183182
The `System.CommandLine` library offers several key benefits:
184183

185-
- Automatic help text generation and validation
186-
- Support for POSIX and Windows command-line conventions
187-
- Built-in tab completion capabilities
188-
- Consistent parsing behavior across applications
184+
- Automatic help text generation and validation.
185+
- Support for POSIX and Windows command-line conventions.
186+
- Built-in tab completion capabilities.
187+
- Consistent parsing behavior across applications.
189188

190189
To add command line parsing capabilities, first add the `System.CommandLine` package. Add this directive after the existing package directive:
191190

@@ -215,7 +214,7 @@ The preceding code validates all command line arguments. If the validation fails
215214

216215
## Use parsed command line results
217216

218-
Now, finish the app to use the parsed options and write the output. First, define a record to hold the parsed options. File-based apps can include type declarations, like records. They must be after all top-level statements and local functions. Add a `record` declaration to store the messages and the delay option value:
217+
Now, finish the app to use the parsed options and write the output. First, define a record to hold the parsed options. File-based apps can include type declarations, like records and classes. They must be after all top-level statements and local functions. Add a `record` declaration to store the messages and the delay option value:
219218

220219
:::code language="csharp" source="./snippets/file-based-programs/AsciiArt.cs" id="Record":::
221220

0 commit comments

Comments
 (0)