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
Copy file name to clipboardExpand all lines: docs/csharp/fundamentals/tutorials/file-based-programs.md
+14-15Lines changed: 14 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,11 +10,10 @@ ai-usage: ai-assisted
10
10
# Tutorial: Build file-based C# programs
11
11
12
12
> [!IMPORTANT]
13
-
>
14
13
> File-based programs are a feature of .NET 10, which is in preview.
15
14
> 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.
16
15
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.
18
17
19
18
In this tutorial, you:
20
19
@@ -45,9 +44,9 @@ Save the file. Then, open the integrated terminal in Visual Studio Code and type
45
44
dotnet run AsciiArt.cs
46
45
```
47
46
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.
49
48
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):
@@ -85,9 +84,9 @@ The `--` option indicates that all following command arguments should be passed
85
84
86
85
This version demonstrates these new concepts:
87
86
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.
91
90
92
91
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:
93
92
@@ -156,13 +155,13 @@ Next, add a package that supports ASCII art, [Colorful.Console](https://www.nuge
156
155
157
156
> [!IMPORTANT]
158
157
>
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.
160
159
161
160
Next, change the lines that call `Console.WriteLine` to use the `Colorful.Console.WriteAscii` method instead:
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:
166
165
167
166
1. Quote multiple words that should be written on one line:
168
167
@@ -176,16 +175,16 @@ Run the program, and you see ASCII art output instead of echoed text. Next, let'
176
175
AsciiArt.cs --delay 1000
177
176
```
178
177
179
-
Users should be able to use both together.
178
+
Users should be able to use both arguments together.
180
179
181
180
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.
182
181
183
182
The `System.CommandLine` library offers several key benefits:
184
183
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.
189
188
190
189
To add command line parsing capabilities, first add the `System.CommandLine` package. Add this directive after the existing package directive:
191
190
@@ -215,7 +214,7 @@ The preceding code validates all command line arguments. If the validation fails
215
214
216
215
## Use parsed command line results
217
216
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:
0 commit comments