[Everyday C#] - Program structure: namespaces + preprocessor directives#52082
[Everyday C#] - Program structure: namespaces + preprocessor directives#52082BillWagner wants to merge 13 commits intodotnet:mainfrom
Conversation
docs/csharp/fundamentals/program-structure/organizing-programs.md
Outdated
Show resolved
Hide resolved
docs/csharp/fundamentals/program-structure/preprocessor-directives.md
Outdated
Show resolved
Hide resolved
d0265d9 to
8d95b30
Compare
adegeo
left a comment
There was a problem hiding this comment.
Submitting my thoughts per our discussion.
| - `return result;` (return statement) | ||
|
|
||
| The key distinction: expressions evaluate to values, while statements perform actions. Some constructs, like method calls, can be both. For example, `Math.Max(a, b)` is an expression when used in `int result = Math.Max(a, b);`, but becomes an expression statement when written alone as `Math.Max(a, b);`. | ||
| Some constructs serve both roles. For example, `Math.Max(a, b)` is an expression when used in `int result = Math.Max(a, b);`, but becomes an expression statement when written alone as `Math.Max(a, b);`. |
There was a problem hiding this comment.
According to our discussion, we might want to delete this statement
| - "global using [C#]" | ||
| - "file-scoped namespace [C#]" | ||
| --- | ||
| # Namespaces and using directives |
There was a problem hiding this comment.
This article weaves between namespaces and using directives. It would probably make more sense to fully explain namespaces and then move on to how using directives help you use namespaces.
| --- | ||
| # Namespaces and using directives | ||
|
|
||
| Namespaces organize C# types into logical groups and prevent naming collisions between types that share the same simple name. Every .NET type belongs to a namespace. Examples include `System.Console`, `System.Collections.Generic.List<T>`, and `System.Threading.Tasks.Task`. You encounter namespaces constantly, whether you're consuming .NET libraries or organizing your own code. |
There was a problem hiding this comment.
This example may want to elaborate on namespaces. The type is Console in the System namespace. The type is List<T> in the System.Collections.Generic namespace. Using some real-world analogy might be useful. Like how a street address 15452 55th ave ne gets you to a specific house. 15451 can be a house on any street, but you navigate to it based on 55th ave ne. Similarly Task is a type and you access it through the namespace System.Threading.Tasks. But maybe there's a better analogy that's not specific to a locale.
1. **Revise** `docs/csharp/fundamentals/program-structure/index.md` - Update frontmatter: set `ms.date` to current date, add `ai-usage: ai-assisted` - Rewrite to lead with a motivating example showing a modern C# program with file-scoped namespaces, global usings, and top-level statements as the default style - Distinguish the three application styles: file-based apps (C# 14), project-based apps with top-level statements, and `Main`-style project-based apps - Update the "Related Sections" links: change the Namespaces link from `../types/namespaces.md` to `namespaces.md` (since it moves into this directory) - Replace or modernize existing snippet references; consider whether existing `snippets/structure/` and `snippets/toplevel-structure/` need updating to latest .NET target and everyday C# features
- Delete `docs/csharp/fundamentals/types/namespaces.md`
- Create new `docs/csharp/fundamentals/program-structure/namespaces.md` with heavily revised content covering:
- File-scoped namespaces (C# 10) as the *default, recommended* style
- Global using directives (C# 10) including implicit usings from the SDK
- Static `using` (C# 6) for importing static members
- Type and namespace aliases (subset) via `using` alias directive
- `extern alias` (brief mention only)
- How namespaces organize code and the `.` delimiter convention
- Link to SDK section for implicit usings detail
- Follow concept → example → concept → example structure
- Target 1000–2000 words (5–10 minute read)
3. **Move + modernize** namespace snippets from `types/snippets/namespaces/` → `program-structure/snippets/namespaces/` - Move all files from `docs/csharp/fundamentals/types/snippets/namespaces/` to `docs/csharp/fundamentals/program-structure/snippets/namespaces/` - Rename snippet region IDs from legacy numeric (`Snippet1`, `Snippet22`, `Snippet23`, `Snippet6`) to CamelCase names (e.g., `FullyQualifiedName`, `UsingDirective`, `ConsoleShorthand`, `DeclareNamespace`, `FileScopedNamespace`) - Modernize code: update `.csproj` target from `net8.0` to latest .NET, use top-level statements or file-based style where appropriate, use everyday C# features (nullable enable, collection expressions if natural) - Add new snippet files for global usings, static using, and alias examples - Delete the old `types/snippets/namespaces/` directory
4. **Create** new `docs/csharp/fundamentals/program-structure/preprocessor-directives.md`
- This is a *fundamentals-level* article (not the language-reference exhaustive docs at `language-reference/preprocessor-directives.md`)
- Cover only the four directives most relevant to everyday development:
- `#if` / `#elif` / `#else` / `#endif` — conditional compilation (with `DEBUG`, `RELEASE`, target framework symbols)
- `#region` / `#endregion` — code organization
- `#nullable enable/disable/restore` — controlling nullable analysis scope
- `#pragma warning disable/restore` — suppressing specific warnings
- Brief mention of `#!` and `#:` for file-based apps (C# 14) with cross-reference to the overview and language reference
- Link to the full `language-reference/preprocessor-directives.md` for complete reference
- Create snippet project at `program-structure/snippets/preprocessor-directives/`
- Add `ai-usage: ai-assisted` to frontmatter
5. **Create** new `docs/csharp/fundamentals/program-structure/organizing-programs.md` - Addresses [dotnet#34836](dotnet#34836) - Content: assemblies, namespaces, and types as organizational tools - Cover how the organizational hierarchy works: solution → projects → assemblies → namespaces → types - Explain naming conventions and how folder structure typically mirrors namespace structure - Show practical examples of organizing a small multi-project solution - Create snippet project at `program-structure/snippets/organizing-programs/` - Add `ai-usage: ai-assisted` to frontmatter
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
I made a few editorial changes while doing a final proofread.
Content review of the index.md file.
69be370 to
6743192
Compare
Fixes #51990
Fixes #34836
Plan: PR 1 — Program structure: namespaces + preprocessor directives
TL;DR: This PR restructures the Program structure section of C# Fundamentals by (1) revising the overview
index.mdto emphasize file-scoped namespaces and global usings as the default style, (2) moving the namespaces article fromtypes/namespaces.mdtoprogram-structure/namespaces.mdwith a major revision to cover file-scoped namespaces (C# 10), global usings (C# 10), static using (C# 6), and type/namespace aliases, (3) creating a new fundamentals-levelpreprocessor-directives.mdcovering#if,#region,#nullable, and#pragma warning, and (4) creating a neworganizing-programs.mdarticle about assemblies, namespaces, and types as organizational tools (addressing #34836). All code snippets use latest .NET, file-based app style where natural, and the full "Everyday C#" feature set. The PR follows concept → example → concept → example structure per the project conventions.Steps
Revise
docs/csharp/fundamentals/program-structure/index.mdms.dateto current date, addai-usage: ai-assistedMain-style project-based apps../types/namespaces.mdtonamespaces.md(since it moves into this directory)snippets/structure/andsnippets/toplevel-structure/need updating to latest .NET target and everyday C# featuresMove + revise
fundamentals/types/namespaces.md→fundamentals/program-structure/namespaces.mddocs/csharp/fundamentals/types/namespaces.mddocs/csharp/fundamentals/program-structure/namespaces.mdwith heavily revised content covering:using(C# 6) for importing static membersusingalias directiveextern alias(brief mention only).delimiter conventionMove + modernize namespace snippets from
types/snippets/namespaces/→program-structure/snippets/namespaces/docs/csharp/fundamentals/types/snippets/namespaces/todocs/csharp/fundamentals/program-structure/snippets/namespaces/Snippet1,Snippet22,Snippet23,Snippet6) to CamelCase names (e.g.,FullyQualifiedName,UsingDirective,ConsoleShorthand,DeclareNamespace,FileScopedNamespace).csprojtarget fromnet8.0to latest .NET, use top-level statements or file-based style where appropriate, use everyday C# features (nullable enable, collection expressions if natural)types/snippets/namespaces/directoryCreate new
docs/csharp/fundamentals/program-structure/preprocessor-directives.mdlanguage-reference/preprocessor-directives.md)#if/#elif/#else/#endif— conditional compilation (withDEBUG,RELEASE, target framework symbols)#region/#endregion— code organization#nullable enable/disable/restore— controlling nullable analysis scope#pragma warning disable/restore— suppressing specific warnings#!and#:for file-based apps (C# 14) with cross-reference to the overview and language referencelanguage-reference/preprocessor-directives.mdfor complete referenceprogram-structure/snippets/preprocessor-directives/ai-usage: ai-assistedto frontmatterCreate new
docs/csharp/fundamentals/program-structure/organizing-programs.mdprogram-structure/snippets/organizing-programs/ai-usage: ai-assistedto frontmatterUpdate
docs/csharp/toc.ymlProgram structuresection (currently lines 38–43), add entries for:Namespaces and using directives→fundamentals/program-structure/namespaces.md(after Top-level statements)Preprocessor directives→fundamentals/program-structure/preprocessor-directives.mdOrganizing programs→fundamentals/program-structure/organizing-programs.mdType systemsection (line 50), remove theNamespacesentry pointing tofundamentals/types/namespaces.mdAdd redirect for old namespaces path in
.openpublishing.redirection.csharp.json"/docs/csharp/fundamentals/types/namespaces.md"→ redirect/dotnet/csharp/fundamentals/program-structure/namespacesUpdate existing redirect targets in
.openpublishing.redirection.csharp.json/dotnet/csharp/fundamentals/types/namespacesto point to/dotnet/csharp/fundamentals/program-structure/namespaces:/docs/csharp/namespaces-and-assemblies.md/docs/csharp/programming-guide/namespaces/how-to-use-the-my-namespace.md/docs/csharp/programming-guide/namespaces/index.md/docs/csharp/programming-guide/namespaces/using-namespaces.mdUpdate cross-references in 5 other articles that link to the old namespaces path:
docs/csharp/fundamentals/program-structure/index.md(line 66): change../types/namespaces.md→namespaces.mddocs/csharp/language-reference/keywords/namespace.md(line 105): change../../fundamentals/types/namespaces.md→../../fundamentals/program-structure/namespaces.mddocs/csharp/language-reference/keywords/using-directive.md(line 190): change../../fundamentals/types/namespaces.md→../../fundamentals/program-structure/namespaces.mddocs/csharp/misc/cs0101.md(line 15): change../fundamentals/types/namespaces.md→../fundamentals/program-structure/namespaces.mddocs/csharp/misc/cs1527.md(line 42): change../fundamentals/types/namespaces.md→../fundamentals/program-structure/namespaces.mdSnippet conventions for all new/revised articles:
program-structure/snippets/{article-name}/(omittingcsharp/vbsubfolder since these articles are underdocs/csharp/).csprojfiles target latest .NET with<Nullable>enable</Nullable><FileScopedNamespace>,<GlobalUsing>,<ConditionalCompilation>)var, collection expressions, nullable reference types, string interpolationInternal previews
Mainmethodsusingdirective