Skip to content

Commit e14cd1c

Browse files
BillWagnergewarrenIEvangelist
authored
Language reference and fundamentals for file based programs (#46900)
* publish spec and add preprocessor reference First commit: Publish the C# speclet for ignored preprocessor directives, and make the updates for the ignored directives used for file based programs. * Add file-based programs to get started * Fix host location And, set +x permissions on hello-world.cs * Include file based programs in the structure Include a description and small example of a file based program in the overview of the general structure of a C# program. * Add file based programs to the command line Add a brief description of file based programs in the command line section. * Updates on top level statements Final edits. * Apply suggestions from code review Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com> Co-authored-by: David Pine <david.pine@microsoft.com> * apply feedback. --------- Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com> Co-authored-by: David Pine <david.pine@microsoft.com>
1 parent b8ba46a commit e14cd1c

File tree

9 files changed

+157
-73
lines changed

9 files changed

+157
-73
lines changed

docfx.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@
6161
"partial-events-and-constructors.md",
6262
"null-conditional-assignment.md",
6363
"extensions.md",
64-
"user-defined-compound-assignment.md"
64+
"user-defined-compound-assignment.md",
65+
"ignored-directives.md"
6566
],
6667
"src": "_csharplang/proposals",
6768
"dest": "csharp/language-reference/proposals",
@@ -533,7 +534,7 @@
533534
"_csharplang/proposals/csharp-11.0/*.md": "09/30/2022",
534535
"_csharplang/proposals/csharp-12.0/*.md": "08/15/2023",
535536
"_csharplang/proposals/csharp-13.0/*.md": "10/31/2024",
536-
"_csharplang/proposals/*.md": "04/08/2025",
537+
"_csharplang/proposals/*.md": "06/19/2025",
537538
"_roslyn/docs/compilers/CSharp/Compiler Breaking Changes - DotNet 7.md": "11/08/2022",
538539
"_roslyn/docs/compilers/CSharp/Compiler Breaking Changes - DotNet 8.md": "11/08/2023",
539540
"_roslyn/docs/compilers/CSharp/Compiler Breaking Changes - DotNet 9.md": "11/09/2024",
@@ -713,6 +714,7 @@
713714
"_csharplang/proposals/null-conditional-assignment.md": "Null conditional assignment",
714715
"_csharplang/proposals/extensions.md": "Extension members",
715716
"_csharplang/proposals/user-defined-compound-assignment.md": "User-defined compound assignment",
717+
"_csharplang/proposals/ignored-directives.md": "Ignored preprocessor directives",
716718
"_roslyn/docs/compilers/CSharp/Compiler Breaking Changes - DotNet 7.md": "C# compiler breaking changes since C# 10",
717719
"_roslyn/docs/compilers/CSharp/Compiler Breaking Changes - DotNet 8.md": "C# compiler breaking changes since C# 11",
718720
"_roslyn/docs/compilers/CSharp/Compiler Breaking Changes - DotNet 9.md": "C# compiler breaking changes since C# 12",
@@ -838,6 +840,7 @@
838840
"_csharplang/proposals/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.",
839841
"_csharplang/proposals/extensions.md": "This proposal enables new kinds of extension members. These new extension members support extension properties, extension static members, including extension operators.",
840842
"_csharplang/proposals/user-defined-compound-assignment.md": "This proposal introduces user-defined compound assignment operators. Developers can override compound assignment, increment, and decrement operators.",
843+
"_csharplang/proposals/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 `#!`",
841844
"_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",
842845
"_roslyn/docs/compilers/CSharp/Compiler Breaking Changes - DotNet 8.md": "Learn about any breaking changes since the initial release of C# 11 and included in C# 12",
843846
"_roslyn/docs/compilers/CSharp/Compiler Breaking Changes - DotNet 9.md": "Learn about any breaking changes since the initial release of C# 12 and included in C# 13",

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,44 @@
11
---
22
title: "General Structure of a Program"
33
description: Learn about the structure of a C# program by using a skeleton program that contains all the required elements for a program.
4-
ms.date: 08/01/2024
4+
ms.date: 06/20/2025
55
helpviewer_keywords:
66
- "C# language, program structure"
7-
ms.assetid: 5ae964a5-0ef0-40fe-88fb-6d1793371d0d
87
---
98
# General Structure of a C# Program
109

1110
C# programs consist of one or more files. Each file contains zero or more namespaces. A namespace contains types such as classes, structs, interfaces, enumerations, and delegates, or other namespaces. The following example is the skeleton of a C# program that contains all of these elements.
1211

1312
:::code language="csharp" source="snippets/toplevel-structure/Program.cs":::
1413

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!");`.
14+
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 text line of program text in that file. In this case, it's the `Console.WriteLine("Hello world!");`.
1615
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:
1716

1817
:::code language="csharp" source="snippets/structure/Program.cs":::
1918

20-
In that case the program will start in the first line of `Main` method, which is `Console.WriteLine("Hello world!");`
19+
In that case the program starts in the opening brace of `Main` method, which is `Console.WriteLine("Hello world!");`
20+
21+
## Building and running C# programs
22+
23+
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.
24+
25+
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`:
26+
27+
:::code language="csharp" source="./snippets/file-based-program/hello-world.cs":::
28+
29+
The first line of the program contains the `#!` sequence for Unix shells. The location of the `dotnet` CLI can vary on different distributions. On any Unix system, if you set the *execute* (`+x`) permission on a C# file, you can run the C# file from the command line:
30+
31+
```bash
32+
./hello-world.cs
33+
```
34+
35+
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.
2136

2237
## Expressions and statements
2338

2439
C# programs are built using *expressions* and *statements*. Expressions produce a value, and statements perform an action:
2540

26-
An *expression* is a combination of values, variables, operators, and method calls that evaluates to a single value. Expressions produce a result and can be used wherever a value is expected. The following examples are expressions:
41+
An *expression* is a combination of values, variables, operators, and method calls that evaluate to a single value. Expressions produce a result and can be used wherever a value is expected. The following examples are expressions:
2742

2843
- `42` (literal value)
2944
- `x + y` (arithmetic operation)

0 commit comments

Comments
 (0)