diff --git a/docfx.json b/docfx.json index a4a2fa1fc0dae..c6ca578234c4c 100644 --- a/docfx.json +++ b/docfx.json @@ -835,7 +835,7 @@ "_csharplang/proposals/csharp-14.0/null-conditional-assignment.md": "This proposal allows the null conditional operator to be used for the destination of assignment expressions. This allows you to assign a value to a property or field only if the left side is not null.", "_csharplang/proposals/csharp-14.0/extensions.md": "This proposal enables new kinds of extension members. These new extension members support extension properties, extension static members, including extension operators.", "_csharplang/proposals/csharp-14.0/user-defined-compound-assignment.md": "This proposal introduces user-defined compound assignment operators. Developers can override compound assignment, increment, and decrement operators.", - "_csharplang/proposals/csharp-14.0/ignored-directives.md": "This proposal allows a source file to include ignored directives. In most cases, ignored directives are used for file based programs, for example `#!`", + "_csharplang/proposals/csharp-14.0/ignored-directives.md": "This proposal allows a source file to include ignored directives. In most cases, ignored directives are used for file-based apps, for example `#!`", "_csharplang/proposals/csharp-14.0/extension-operators.md": "This proposal extends the proposal for extensions to include *extension operators*, where an operator can be an extension member.", "_csharplang/proposals/csharp-14.0/named-and-optional-parameters-in-expression-trees.md": "This proposal allows an expression tree to include named and optional parameters. This enables expression trees to be more flexible in how they are constructed.", "_roslyn/docs/compilers/CSharp/Compiler Breaking Changes - DotNet 7.md": "Learn about any breaking changes since the initial release of C# 10 and included in C# 11", diff --git a/docs/core/whats-new/dotnet-10/sdk.md b/docs/core/whats-new/dotnet-10/sdk.md index 6eb8d37e46818..d1ad48ef454c2 100644 --- a/docs/core/whats-new/dotnet-10/sdk.md +++ b/docs/core/whats-new/dotnet-10/sdk.md @@ -113,7 +113,7 @@ The output provides a structured, machine-readable description of the command's ## File-based apps enhancements -.NET 10 brings significant updates to the file-based apps experience, including publish support and native AOT capabilities. For an introduction to file-based programs, see [File based programs](../../../csharp/tour-of-csharp/overview.md#file-based-programs) and [Building and running C# programs](../../../csharp/fundamentals/program-structure/index.md#building-and-running-c-programs). +.NET 10 brings significant updates to the file-based apps experience, including publish support and native AOT capabilities. For an introduction to file-based apps, see [File-based apps](../../../csharp/tour-of-csharp/overview.md#file-based-apps) and [Building and running C# programs](../../../csharp/fundamentals/program-structure/index.md#building-and-running-c-programs). ### Enhanced file-based apps with publish support and native AOT diff --git a/docs/csharp/fundamentals/program-structure/index.md b/docs/csharp/fundamentals/program-structure/index.md index 33067c5d2802f..5a8a0a88ac7ca 100644 --- a/docs/csharp/fundamentals/program-structure/index.md +++ b/docs/csharp/fundamentals/program-structure/index.md @@ -22,7 +22,7 @@ In that case the program starts in the opening brace of `Main` method, which is C# is a *compiled* language. In most C# programs, you use the [`dotnet build`](../../../core/tools/dotnet-build.md) command to compile a group of source files into a binary package. Then, you use the [`dotnet run`](../../../core/tools/dotnet-run.md) command to run the program. (You can simplify this process because `dotnet run` compiles the program before running it if necessary.) These tools support a rich language of configuration options and command-line switches. The `dotnet` command line interface (CLI), which is included in the .NET SDK, provides many [tools](../../../core/tools/index.md) to generate and modify C# files. -Beginning with C# 14 and .NET 10, you can create *file based programs*, which simplifies building and running C# programs. You use the `dotnet run` command to run a program contained in a single `*.cs` file. For example, if the following snippet is stored in a file named `hello-world.cs`, you can run it by typing `dotnet run hello-world.cs`: +Beginning with C# 14 and .NET 10, you can create *file-based apps*, which simplifies building and running C# programs. You use the `dotnet run` command to run a program contained in a single `*.cs` file. For example, if the following snippet is stored in a file named `hello-world.cs`, you can run it by typing `dotnet run hello-world.cs`: :::code language="csharp" source="./snippets/file-based-program/hello-world.cs"::: @@ -32,7 +32,7 @@ The first line of the program contains the `#!` sequence for Unix shells. The lo ./hello-world.cs ``` -The source for these programs must be a single file, but otherwise all C# syntax is valid. You can use file based programs for small command-line utilities, prototypes, or other experiments. File based programs allow [preprocessor directives](../../language-reference/preprocessor-directives.md#file-based-programs) that configure the build system. +The source for these programs must be a single file, but otherwise all C# syntax is valid. You can use file-based apps for small command-line utilities, prototypes, or other experiments. file-based apps allow [preprocessor directives](../../language-reference/preprocessor-directives.md#file-based-apps) that configure the build system. ## Expressions and statements diff --git a/docs/csharp/fundamentals/program-structure/main-command-line.md b/docs/csharp/fundamentals/program-structure/main-command-line.md index e32e9ed00df2f..9821df757dc56 100644 --- a/docs/csharp/fundamentals/program-structure/main-command-line.md +++ b/docs/csharp/fundamentals/program-structure/main-command-line.md @@ -24,7 +24,7 @@ You can also use top-level statements in one file as the entry point for your ap :::code language="csharp" source="snippets/top-level-statements-1/Program.cs"::: -Beginning with C# 14, programs can be [*file based programs*](./index.md#building-and-running-c-programs), where a single file contains the program. You run *file based programs* with the command `dotnet run `, or using the `#!/usr/local/share/dotnet/dotnet run` directive as the first line (unix shells only). +Beginning with C# 14, programs can be [*file-based apps*](./index.md#building-and-running-c-programs), where a single file contains the program. You run *file-based apps* with the command `dotnet run `, or using the `#!/usr/local/share/dotnet/dotnet run` directive as the first line (unix shells only). ## Overview diff --git a/docs/csharp/fundamentals/program-structure/top-level-statements.md b/docs/csharp/fundamentals/program-structure/top-level-statements.md index 3396d27a69988..743c695a5301e 100644 --- a/docs/csharp/fundamentals/program-structure/top-level-statements.md +++ b/docs/csharp/fundamentals/program-structure/top-level-statements.md @@ -85,7 +85,7 @@ The compiler generates a method to serve as the program entry point for a projec | `return` | `static int Main(string[] args)` | | No `await` or `return` | `static void Main(string[] args)` | -Beginning with C# 14, programs can be [*file based programs*](./index.md#building-and-running-c-programs), where a single file contains the program. You run *file based programs* with the command `dotnet run `, or using the `#!/usr/local/share/dotnet/dotnet run` directive as the first line (unix shells only). +Beginning with C# 14, programs can be [*file-based apps*](./index.md#building-and-running-c-programs), where a single file contains the program. You run *file-based apps* with the command `dotnet run `, or using the `#!/usr/local/share/dotnet/dotnet run` directive as the first line (unix shells only). ## C# language specification diff --git a/docs/csharp/fundamentals/tutorials/file-based-programs.md b/docs/csharp/fundamentals/tutorials/file-based-programs.md new file mode 100644 index 0000000000000..6374c67570c23 --- /dev/null +++ b/docs/csharp/fundamentals/tutorials/file-based-programs.md @@ -0,0 +1,276 @@ +--- +title: Build file-based apps +description: File-based apps are command line utilities that are built and execute without a project file. The build and run commands are implicit. New syntax supports project settings in source. +ms.date: 08/14/2025 +ms.topic: tutorial +ai-usage: ai-assisted +#customer intent: As a developer, I want build utilities so that more work is automated. +--- + +# Tutorial: Build file-based C# programs + +> [!IMPORTANT] +> File-based apps are a feature of .NET 10, which is in preview. +> 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. + +*File-based apps* are programs contained within a single `*.cs` file that are built and run without a corresponding project (`*.csproj`) file. File-based apps are ideal for learning C# because they have less complexity: The entire program is stored in a single file. File-based apps are also useful for building command line utilities. On Unix platforms, file-based apps can be run using `#!` (shebang) directives. + +In this tutorial, you: + +> [!div class="checklist"] +> +> * Create a file-based program. +> * Add Unix shebang (`#!`) support. +> * Read command line arguments. +> * Handle standard input. +> * Write ASCII art output. +> * Process command line arguments. +> * Use parsed command line results. +> * Test the final application. + +You build a file-based program that writes text as ASCII art. The app is contained in a single file, uses NuGet packages that implement some of the core features. + +## Prerequisites + +- The .NET 10 preview SDK. Download it from the [.NET download site](https://dotnet.microsoft.com/download/dotnet/10.0). +- Visual Studio Code. Download it from the [Visual Studio Code homepage](https://code.visualstudio.com/Download). +- (Optional) The C# DevKit extension for Visual Studio Code. Download it from the [Visual Studio Code marketplace](https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.csdevkit). + +## Create a file-based program + +1. Open Visual Studio Code and create a new file named `AsciiArt.cs`. Enter the following text: + + ```csharp + Console.WriteLine("Hello, world!"); + ``` + +1. Save the file. Then, open the integrated terminal in Visual Studio Code and type: + + ```dotnetcli + dotnet run AsciiArt.cs + ``` + +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 preceding steps demonstrate that file-based apps 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): + +```dotnetcli +AsciiArt succeeded (7.3s) → AppData\Local\Temp\dotnet\runfile\AsciiArt-85c58ae0cd68371711f06f297fa0d7891d0de82afde04d8c64d5f910ddc04ddc\bin\debug\AsciiArt.dll +``` + +On unix platforms, the output folder is something similar to: + +```dotnetcli +AsciiArt succeeded (7.3s) → Library/Application Support/dotnet/runfile/AsciiArt-85c58ae0cd68371711f06f297fa0d7891d0de82afde04d8c64d5f910ddc04ddc/bin/debug/AsciiArt.dll +``` + +That output tells you where the temporary files and build outputs are placed. Throughout this tutorial, anytime you edit the source file, the `dotnet` host updates the executable before it runs. + +File-based apps are regular C# programs. The only limitation is that they must be written in one source file. You can use top-level statements or a classic `Main` method as an entry point. You can declare any types: classes, interfaces, and structs. You can structure the algorithms in a file-based program the same as you would in any C# program. You can even declare multiple namespaces to organize your code. If you find a file-based program is growing too large for a single file, you can convert it to a project based program and split the source into multiple files. File-based apps are a great prototyping tool. You can start experimenting with minimal overhead to prove concepts and build algorithms. + +## Unix shebang (`#!`) support + +> [!NOTE] +> +> Support for `#!` directives applies on unix platforms only. There isn't a similar directive for Windows to directly execute a C# program. On Windows, you must use `dotnet run` on the command line. + +On unix, you can run file-based apps directly, typing the source file name on the command line instead of `dotnet run`. You need to make two changes: + +1. Set *execute* permissions on the source file: + + ```bash + chmod +x AsciiArt.cs + ``` + +2. Add a shebang (`#!`) directive as the first line of the `AsciiArt.cs` file: + + ```csharp + #!/usr/local/share/dotnet/dotnet run + ``` + +The location of `dotnet` can be different on different unix installations. Use the command `whence dotnet` to local the `dotnet` host in your environment. + +After making these two changes, you can run the program from the command line directly: + +```bash +./AsciiArt.cs +``` + +If you prefer, you can remove the extension so you can type `./AsciiArt` instead. You can add the `#!` to your source file even if you use Windows. The Windows command line doesn't support `#!`, but the C# compiler allows that directive in file-based apps on all platforms. + +## Read command line arguments + +Now, write all arguments on the command line to the output. + +1. Replace the current contents of `AsciiArt.cs` with the following code: + + ```csharp + if (args.Length > 0) + { + string message = string.Join(' ', args); + Console.WriteLine(message); + } + ``` + +1. You can run this version by typing the following command: + + ```dotnetcli + dotnet run AsciiArt.cs -- This is the command line. + ``` + + The `--` option indicates that all following command arguments should be passed to the AsciiArt program. The arguments `This is the command line.` are passed as an array of strings, where each string is one word: `This`, `is`, `the`, `command`, and `line.`. + +This version demonstrates these new concepts: + +- 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. +- The [`string.Join`](xref:System.String.Join*) method joins multiple strings into a single string, with the specified separator. In this case, the separator is a single space. +- writes the string to the standard output console, followed by a new line. + +## Handle standard input + +That handles command line arguments correctly. Now, add the code to handle reading input from standard input (`stdin`) instead of command line arguments. + +1. Add the following `else` clause to the `if` statement you added in the preceding code: + + ```csharp + else + { + while (Console.ReadLine() is string line && line.Length > 0) + { + Console.WriteLine(line); + } + } + ``` + + The preceding code reads the console input until either a blank line or a `null` is read. (The method returns `null` if the input stream is closed by typing ctrl+C.) + +1. Test reading standard input by creating a new text file in the same folder. Name the file `input.txt` and add the following lines: + + :::code language="txt" source="./snippets/file-based-programs/input.txt"::: + + Keep the lines short so they format correctly when you add the feature to use ASCII art. + +1. Run the program again. + + With bash: + + ```bash + cat input.txt | dotnet run AsciiArt.cs + ``` + + Or, with PowerShell: + + ```powershell + Get-Content input.txt | dotnet run AsciiArt.cs + ``` + +Now your program can accept either command line arguments or standard input. + +## Write ASCII Art output + +Next, add a package that supports ASCII art, [Colorful.Console](https://www.nuget.org/packages/Colorful.Console). To add a package to a file-based program, you use the `#:package` directive. + +1. Add the following directive after the `#!` directive in your AsciiArt.cs file: + + :::code language="csharp" source="./snippets/file-based-programs/AsciiArt.cs" id="ColorfulPackage"::: + + > [!IMPORTANT] + > 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. + +1. Change the lines that call `Console.WriteLine` to use the `Colorful.Console.WriteAscii` method instead: + + :::code language="csharp" source="./snippets/file-based-programs/AsciiArt.cs" id="WriteAscii"::: + +1. Run the program, and you see ASCII art output instead of echoed text. + +## Process command options + +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: + +1. Quote multiple words that should be written on one line: + + ```dotnetcli + AsciiArt.cs "This is line one" "This is another line" "This is the last line" + ``` + +1. Add a `--delay` option to pause between each line: + + ```dotnetcli + AsciiArt.cs --delay 1000 + ``` + +Users should be able to use both arguments together. + +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. + +The `System.CommandLine` library offers several key benefits: + +- Automatic help text generation and validation. +- Support for POSIX and Windows command-line conventions. +- Built-in tab completion capabilities. +- Consistent parsing behavior across applications. + +1. Add the `System.CommandLine` package. Add this directive after the existing package directive: + + :::code language="csharp" source="./snippets/file-based-programs/AsciiArt.cs" id="CommandLinePackage"::: + + > [!IMPORTANT] + > The version `2.0.0-beta6` 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. Check the package's [NuGet page](https://www.nuget.org/packages/System.CommandLine) for the latest version to ensure you use a package version with the latest security fixes. + +1. Add the necessary using statements at the top of your file (after the `#!` and `#:package` directives): + + :::code language="csharp" source="./snippets/file-based-programs/AsciiArt.cs" id="Usings"::: + +1. Define the delay option and messages argument. Add the following code to create the `CommandLine.Option` and `CommandLine.Argument` objects to represent the command line option and argument: + + :::code language="csharp" source="./snippets/file-based-programs/AsciiArt.cs" id="OptionArgument"::: + + In command-line applications, options typically begin with `--` (double dash) and can accept arguments. The `--delay` option accepts an integer argument that specifies the delay in milliseconds. The `messagesArgument` defines how any remaining tokens after options are parsed as text. Each token becomes a separate string in the array, but text can be quoted to include multiple words in one token. For example, `"This is one message"` becomes a single token, while `This is four tokens` becomes four separate tokens. + + The preceding code defines the argument type for the `--delay` option, and that the arguments are an array of `string` values. This application has only one command, so you use the *root command*. + +1. Create a root command and configure it with the option and argument. Add the argument and option to the root command: + + :::code language="csharp" source="./snippets/file-based-programs/AsciiArt.cs" id="RootCommand"::: + +1. Add the code to parse the command line arguments and handle any errors. This code validates the command line arguments and stores parsed arguments in the object: + + :::code language="csharp" source="./snippets/file-based-programs/AsciiArt.cs" id="ParseAndValidate"::: + +The preceding code validates all command line arguments. If the validation fails, errors are written to the console, and the app exits. + +## Use parsed command line results + +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. + +1. Add a [`record`](../types/records.md) declaration to store the messages and the delay option value: + + :::code language="csharp" source="./snippets/file-based-programs/AsciiArt.cs" id="Record"::: + +1. Add the following local function before the record declaration. This method handles both command line arguments and standard input, and returns a new record instance: + + :::code language="csharp" source="./snippets/file-based-programs/AsciiArt.cs" id="ProcessParsedArgs"::: + +1. Create a local function to write the ASCII art with the specified delay. This function writes each message in the record with the specified delay between each message: + + :::code language="csharp" source="./snippets/file-based-programs/AsciiArt.cs" id="WriteAscii"::: + +1. Replace the `if` clause you wrote earlier with the following code that processes the command line arguments and write the output: + +:::code language="csharp" source="./snippets/file-based-programs/AsciiArt.cs" id="InvokeCommand"::: + +You created a `record` type that provides structure to the parsed command line options and arguments. New local functions create an instance of the record, and use the record to write the ASCII art output. + +## Test the final application + +Test the application by running several different commands. If you have trouble, here's the finished sample to compare with what you built: + +:::code language="csharp" source="./snippets/file-based-programs/AsciiArt"::: + +In this tutorial, you learned to build a file-based program, where you build the program in a single C# file. These programs don't use a project file, and can use the `#!` directive on unix systems. Learners can create these programs after trying our [online tutorials](../../tour-of-csharp/tutorials/hello-world.md) and before building larger project-based apps. File-based apps are also a great platform for command line utilities. + +## Related content + +- [Top level statement](../program-structure/top-level-statements.md) +- [Preprocessor directives](../../language-reference/preprocessor-directives.md#file-based-apps) +- [What's new in C# 14](../../whats-new/csharp-14.md) diff --git a/docs/csharp/fundamentals/tutorials/snippets/file-based-programs/AsciiArt b/docs/csharp/fundamentals/tutorials/snippets/file-based-programs/AsciiArt new file mode 100755 index 0000000000000..78bb5e08a7b4c --- /dev/null +++ b/docs/csharp/fundamentals/tutorials/snippets/file-based-programs/AsciiArt @@ -0,0 +1,67 @@ +#!/usr/local/share/dotnet/dotnet run + +#:package Colorful.Console@1.2.15 +#:package System.CommandLine@2.0.0-beta6 + +using System.CommandLine; +using System.CommandLine.Parsing; + +Option delayOption = new("--delay") +{ + Description = "Delay between lines, specified as milliseconds.", + DefaultValueFactory = parseResult => 100 +}; + +Argument messagesArgument = new("Messages") +{ + Description = "Text to render." +}; + +RootCommand rootCommand = new("Ascii Art file-based program sample"); + +rootCommand.Options.Add(delayOption); +rootCommand.Arguments.Add(messagesArgument); + +ParseResult result = rootCommand.Parse(args); +foreach (ParseError parseError in result.Errors) +{ + Console.Error.WriteLine(parseError.Message); +} +if (result.Errors.Count > 0) +{ + return 1; +} + +var parsedArgs = await ProcessParseResults(result); + +await WriteAsciiArt(parsedArgs); +return 0; + +async Task ProcessParseResults(ParseResult result) +{ + int delay = result.GetValue(delayOption); + List messages = [.. result.GetValue(messagesArgument) ?? Array.Empty()]; + + if (messages.Count == 0) + { + while (Console.ReadLine() is string line && line.Length > 0) + { + // + Colorful.Console.WriteAscii(line); + // + await Task.Delay(delay); + } + } + return new([.. messages], delay); +} + +async Task WriteAsciiArt(AsciiMessageOptions options) +{ + foreach (string message in options.Messages) + { + Colorful.Console.WriteAscii(message); + await Task.Delay(options.Delay); + } +} + +public record AsciiMessageOptions(string[] Messages, int Delay); diff --git a/docs/csharp/fundamentals/tutorials/snippets/file-based-programs/AsciiArt.cs b/docs/csharp/fundamentals/tutorials/snippets/file-based-programs/AsciiArt.cs new file mode 100755 index 0000000000000..24ba5db893246 --- /dev/null +++ b/docs/csharp/fundamentals/tutorials/snippets/file-based-programs/AsciiArt.cs @@ -0,0 +1,87 @@ +#!/usr/local/share/dotnet/dotnet run + +// +#:package Colorful.Console@1.2.15 +// +// +#:package System.CommandLine@2.0.0-beta6 +// + +// +using System.CommandLine; +using System.CommandLine.Parsing; +// + +// +Option delayOption = new("--delay") +{ + Description = "Delay between lines, specified as milliseconds.", + DefaultValueFactory = parseResult => 100 +}; + +Argument messagesArgument = new("Messages") +{ + Description = "Text to render." +}; +// + +// +RootCommand rootCommand = new("Ascii Art file-based program sample"); + +rootCommand.Options.Add(delayOption); +rootCommand.Arguments.Add(messagesArgument); +// + +// +ParseResult result = rootCommand.Parse(args); +foreach (ParseError parseError in result.Errors) +{ + Console.Error.WriteLine(parseError.Message); +} +if (result.Errors.Count > 0) +{ + return 1; +} +// + +// +var parsedArgs = await ProcessParseResults(result); + +await WriteAsciiArt(parsedArgs); +return 0; +// + +// +async Task ProcessParseResults(ParseResult result) +{ + int delay = result.GetValue(delayOption); + List messages = [.. result.GetValue(messagesArgument) ?? Array.Empty()]; + + if (messages.Count == 0) + { + while (Console.ReadLine() is string line && line.Length > 0) + { + // + Colorful.Console.WriteAscii(line); + // + await Task.Delay(delay); + } + } + return new([.. messages], delay); +} +// + +// +async Task WriteAsciiArt(AsciiMessageOptions options) +{ + foreach (string message in options.Messages) + { + Colorful.Console.WriteAscii(message); + await Task.Delay(options.Delay); + } +} +// + +// +public record AsciiMessageOptions(string[] Messages, int Delay); +// diff --git a/docs/csharp/fundamentals/tutorials/snippets/file-based-programs/input.txt b/docs/csharp/fundamentals/tutorials/snippets/file-based-programs/input.txt new file mode 100644 index 0000000000000..43df299799bc3 --- /dev/null +++ b/docs/csharp/fundamentals/tutorials/snippets/file-based-programs/input.txt @@ -0,0 +1,10 @@ +Hello from ... +dotnet! + +You can create +file-based apps +in .NET 10 and +C# 14 + +Have fun writing +useful utilities \ No newline at end of file diff --git a/docs/csharp/language-reference/preprocessor-directives.md b/docs/csharp/language-reference/preprocessor-directives.md index 1c4057ff78a31..ec1d766d85caa 100644 --- a/docs/csharp/language-reference/preprocessor-directives.md +++ b/docs/csharp/language-reference/preprocessor-directives.md @@ -47,9 +47,9 @@ helpviewer_keywords: Although the compiler doesn't have a separate preprocessor, the directives described in this section are processed as if there were one. You use them to help in conditional compilation. Unlike C and C++ directives, you can't use these directives to create macros. A preprocessor directive must be the only instruction on a line. -## File based programs +## File-based apps -*File based programs* are programs that are compiled and run using `dotnet run Program.cs` (or any `*.cs` file). The C# compiler ignores these preprocessor directives, but the build system parses them to produce the output. These directives generate warnings when encountered in a project-based compilation. +*File-based apps* are programs that are compiled and run using `dotnet run Program.cs` (or any `*.cs` file). The C# compiler ignores these preprocessor directives, but the build system parses them to produce the output. These directives generate warnings when encountered in a project-based compilation. The C# compiler ignores any preprocessor directive that starts with `#:` or `#!`. @@ -62,7 +62,7 @@ Console.WriteLine("Hello"); The preceding code snippet informs a Unix shell to execute the file using `/usr/local/share/dotnet/dotnet run`. (Your installation directory for the `dotnet` CLI can be different on different Unix or macOS distributions). The `#!` line must be the first line in the file, and the following tokens are the program to run. You need to enable the *execute* (`x`) permission on the C# file for that feature. -The `#:` directives that are used in file based programs include: +The `#:` directives that are used in file-based apps include: - `#:sdk`: diff --git a/docs/csharp/toc.yml b/docs/csharp/toc.yml index 7a89acd91ef69..a97f1a3196ae7 100644 --- a/docs/csharp/toc.yml +++ b/docs/csharp/toc.yml @@ -108,6 +108,8 @@ items: href: fundamentals/coding-style/coding-conventions.md - name: Tutorials items: + - name: Build file-based apps + href: fundamentals/tutorials/file-based-programs.md - name: "How to display command-line arguments" href: fundamentals/tutorials/how-to-display-command-line-arguments.md # TODO: Command line processing tutorials diff --git a/docs/csharp/tour-of-csharp/overview.md b/docs/csharp/tour-of-csharp/overview.md index 76d4fbaa509e0..582c430ef2d1f 100644 --- a/docs/csharp/tour-of-csharp/overview.md +++ b/docs/csharp/tour-of-csharp/overview.md @@ -34,11 +34,11 @@ The `Program` class declared by the "Hello, World" program has a single member, > [!TIP] > The examples in this article give you a first look at C# code. Some samples might show elements of C# that you're not familiar with. When you're ready to learn C#, start with our [beginner tutorials](./tutorials/index.md), or dive into the links in each section. If you're experienced in [Java](./tips-for-java-developers.md), [JavaScript](./tips-for-javascript-developers.md), [TypeScript](./tips-for-javascript-developers.md), or [Python](./tips-for-python-developers.md), read our tips to help you find the information you need to quickly learn C#. -## File based programs +## File-based apps C# is a *compiled* language. In most C# programs, you use the [`dotnet build`](../../core/tools/dotnet-build.md) command to compile a group of source files into a binary package. Then, you use the [`dotnet run`](../../core/tools/dotnet-run.md) command to run the program. (You can simplify this process because `dotnet run` compiles the program before running it if necessary.) These tools support a rich language of configuration options and command-line switches. The `dotnet` command line interface (CLI), which is included in the .NET SDK, provides many [tools](../../core/tools/index.md) to generate and modify C# files. -Beginning with C# 14 and .NET 10, you can create *file based programs*, which simplifies building and running C# programs. You use the `dotnet run` command to run a program contained in a single `*.cs` file. For example, if the following code is stored in a file named `hello-world.cs`, you can run it by typing `dotnet run hello-world.cs`: +Beginning with C# 14 and .NET 10, you can create *file-based apps*, which simplifies building and running C# programs. You use the `dotnet run` command to run a program contained in a single `*.cs` file. For example, if the following code is stored in a file named `hello-world.cs`, you can run it by typing `dotnet run hello-world.cs`: :::code language="csharp" source="./snippets/file-based-programs/hello-world.cs"::: @@ -48,7 +48,7 @@ The first line of the program contains the `#!` sequence (shebang) for unix shel ./hello-world.cs ``` -The source for these programs must be a single file, but otherwise all C# syntax is valid. You can use file based programs for small command-line utilities, prototypes, or other experiments. +The source for these programs must be a single file, but otherwise all C# syntax is valid. You can use file-based apps for small command-line utilities, prototypes, or other experiments. ## Familiar C# features