Skip to content

Commit 33a50a6

Browse files
committed
Phase 3: Snippets
. New snippet projects under `docs/csharp/fundamentals/null-safety/snippets/{nullable-reference-types,resolve-warnings,migration-strategies}/`. Each minimal `.csproj`, `<Nullable>enable</Nullable>`, latest TFM. Build + execute per docs `copilot-instructions.md`. 6. Move `docs/csharp/tutorials/snippets/NullableIntroduction/` → `docs/csharp/fundamentals/tutorials/snippets/NullableIntroduction/`. Update tutorial `:::code:::` source paths.
1 parent a54423c commit 33a50a6

6 files changed

Lines changed: 8 additions & 8 deletions

File tree

docs/csharp/fundamentals/tutorials/nullable-reference-types.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ The `surveyQuestions` field is a non-nullable `List<SurveyQuestion>`. Initializi
9191

9292
In `Program.cs`, create a `SurveyRun` and add three questions:
9393

94-
:::code language="csharp" source="snippets/NullableIntroduction/Program.cs" id="AddQuestions":::
94+
:::code language="csharp" source="snippets/NullableIntroduction/Program.cs" id="SnippetAddQuestions":::
9595

9696
To see how the compiler enforces non-nullable parameters, try adding the following line and rebuilding:
9797

@@ -120,41 +120,41 @@ public class SurveyResponse
120120

121121
Add a static factory method that creates respondents with a random ID:
122122

123-
:::code language="csharp" source="snippets/NullableIntroduction/SurveyResponse.cs" id="Random":::
123+
:::code language="csharp" source="snippets/NullableIntroduction/SurveyResponse.cs" id="SnippetRandom":::
124124

125125
Next, add the method that asks the survey to a respondent. Store the answers in a nullable dictionary so the type itself communicates that the respondent might decline:
126126

127-
:::code language="csharp" source="snippets/NullableIntroduction/SurveyResponse.cs" id="AnswerSurvey":::
127+
:::code language="csharp" source="snippets/NullableIntroduction/SurveyResponse.cs" id="SnippetAnswerSurvey":::
128128

129129
The `surveyResponses` field is `Dictionary<int, string>?`. Anywhere the field is dereferenced without first checking against `null`, the compiler issues a warning. Inside `AnswerSurvey`, the compiler tracks that `surveyResponses` is *not-null* immediately after the `new` expression, so the loop body needs no extra check.
130130

131131
Add a method on `SurveyRun` that builds up a list of respondents until enough consent to participate:
132132

133-
:::code language="csharp" source="snippets/NullableIntroduction/SurveyRun.cs" id="PerformSurvey":::
133+
:::code language="csharp" source="snippets/NullableIntroduction/SurveyRun.cs" id="SnippetPerformSurvey":::
134134

135135
The `respondents` field is `List<SurveyResponse>?`—it's `null` until the survey runs.
136136

137137
Call `PerformSurvey` from `Main`:
138138

139-
:::code language="csharp" source="snippets/NullableIntroduction/Program.cs" id="RunSurvey":::
139+
:::code language="csharp" source="snippets/NullableIntroduction/Program.cs" id="SnippetRunSurvey":::
140140

141141
## Examine the survey results
142142

143143
To report results, expose a few helpers from `SurveyResponse` and `SurveyRun`. On `SurveyResponse`, add expression-bodied members that handle the nullable dictionary:
144144

145-
:::code language="csharp" source="snippets/NullableIntroduction/SurveyResponse.cs" id="SurveyStatus":::
145+
:::code language="csharp" source="snippets/NullableIntroduction/SurveyResponse.cs" id="SnippetSurveyStatus":::
146146

147147
`AnsweredSurvey` checks the field against `null`. `Answer` uses the `?.` operator to dereference safely and the `??` operator to substitute a non-null fallback. The method's return type is non-nullable `string`, so callers don't need null checks.
148148

149149
On `SurveyRun`, add expression-bodied members that expose the list of participants and questions:
150150

151-
:::code language="csharp" source="snippets/NullableIntroduction/SurveyRun.cs" id="RunReport":::
151+
:::code language="csharp" source="snippets/NullableIntroduction/SurveyRun.cs" id="SnippetRunReport":::
152152

153153
`AllParticipants` returns a non-nullable sequence even though `respondents` might be `null`. The `??` operator substitutes `Enumerable.Empty<SurveyResponse>()` when the field hasn't been populated yet. If you remove the `??` clause, the compiler warns that the method might return `null` despite a non-nullable return type.
154154

155155
Finally, write the report at the bottom of `Main`:
156156

157-
:::code language="csharp" source="snippets/NullableIntroduction/Program.cs" id="WriteAnswers":::
157+
:::code language="csharp" source="snippets/NullableIntroduction/Program.cs" id="SnippetWriteAnswers":::
158158

159159
Notice that no null check is needed for `participant`, `surveyRun.Questions`, or `surveyRun.GetQuestion(i)`. The types declare those values as non-nullable, so the compiler treats them as *not-null* throughout the loop.
160160

docs/csharp/tutorials/snippets/NullableIntroduction/NullableIntroduction.csproj renamed to docs/csharp/fundamentals/tutorials/snippets/NullableIntroduction/NullableIntroduction.csproj

File renamed without changes.

docs/csharp/tutorials/snippets/NullableIntroduction/Program.cs renamed to docs/csharp/fundamentals/tutorials/snippets/NullableIntroduction/Program.cs

File renamed without changes.

docs/csharp/tutorials/snippets/NullableIntroduction/SurveyQuestion.cs renamed to docs/csharp/fundamentals/tutorials/snippets/NullableIntroduction/SurveyQuestion.cs

File renamed without changes.

docs/csharp/tutorials/snippets/NullableIntroduction/SurveyResponse.cs renamed to docs/csharp/fundamentals/tutorials/snippets/NullableIntroduction/SurveyResponse.cs

File renamed without changes.

docs/csharp/tutorials/snippets/NullableIntroduction/SurveyRun.cs renamed to docs/csharp/fundamentals/tutorials/snippets/NullableIntroduction/SurveyRun.cs

File renamed without changes.

0 commit comments

Comments
 (0)