Skip to content

Commit 63c0fa2

Browse files
BillWagnerRon Petrusha
authored andcommitted
Create sample for Intro To Nullable references (#491)
* first draft of code sample Contributes to dotnet/docs#8208 * add snippet markers
1 parent c0cd9db commit 63c0fa2

File tree

6 files changed

+204
-0
lines changed

6 files changed

+204
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.28315.86
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NullableIntroduction", "NullableIntroduction\NullableIntroduction.csproj", "{B030A6FA-113E-4AC1-BDE4-7CF1DB29E22C}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{B030A6FA-113E-4AC1-BDE4-7CF1DB29E22C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{B030A6FA-113E-4AC1-BDE4-7CF1DB29E22C}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{B030A6FA-113E-4AC1-BDE4-7CF1DB29E22C}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{B030A6FA-113E-4AC1-BDE4-7CF1DB29E22C}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {47456933-59EE-408A-AD9F-1887183AFCF8}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.2</TargetFramework>
6+
<LangVersion>8.0</LangVersion>
7+
</PropertyGroup>
8+
9+
</Project>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
3+
#nullable enable
4+
namespace NullableIntroduction
5+
{
6+
class Program
7+
{
8+
static void Main(string[] args)
9+
{
10+
// <SnippetAddQuestions>
11+
var surveyRun = new SurveyRun();
12+
surveyRun.AddQuestion(QuestionType.YesNo, "Has your code ever thrown a NullReferenceException?");
13+
surveyRun.AddQuestion(new SurveyQuestion(QuestionType.Number, "How many times (to the nearest 100) has that happened?"));
14+
surveyRun.AddQuestion(QuestionType.Text, "What is your favorite color?");
15+
// </SnippetAddQuestions>
16+
17+
// <SnippetRunSurvey>
18+
surveyRun.PerformSurvey(50);
19+
// </SnippetRunSurvey>
20+
21+
// <SnippetWriteAnswers>
22+
foreach (var participant in surveyRun.AllParticipants)
23+
{
24+
Console.WriteLine($"Participant: {participant.Id}:");
25+
if (participant.AnsweredSurvey)
26+
{
27+
for (int i = 0; i < surveyRun.Questions.Count; i++)
28+
{
29+
var answer = participant.Answer(i);
30+
Console.WriteLine($"\t{surveyRun.GetQuestion(i)} : {answer}");
31+
}
32+
}
33+
else
34+
Console.WriteLine("\tNo responses");
35+
}
36+
// </SnippetWriteAnswers>
37+
}
38+
}
39+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#nullable enable
2+
namespace NullableIntroduction
3+
{
4+
public enum QuestionType
5+
{
6+
YesNo,
7+
Number,
8+
Text
9+
}
10+
11+
public class SurveyQuestion
12+
{
13+
public string QuestionText { get; }
14+
public QuestionType TypeOfQuestion { get; }
15+
16+
public SurveyQuestion(QuestionType typeOfQuestion, string text) =>
17+
(TypeOfQuestion, QuestionText) = (typeOfQuestion, text);
18+
}
19+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
#nullable enable
6+
namespace NullableIntroduction
7+
{
8+
public class SurveyResponse
9+
{
10+
// <SnippetRandom>
11+
private static readonly Random randomGenerator = new Random();
12+
public static SurveyResponse GetRandomId() => new SurveyResponse(randomGenerator.Next());
13+
// </SnippetRandom>
14+
15+
public int Id { get; }
16+
17+
// <SnippetSurveyStatus>
18+
public bool AnsweredSurvey => surveyResponses != null;
19+
public string Answer(int index) => surveyResponses.GetValueOrDefault(index, "No answer")!;
20+
// </SnippetSurveyStatus>
21+
22+
public SurveyResponse(int id) => Id = id;
23+
24+
// <SnippetAnswerSurvey>
25+
private Dictionary<int, string>? surveyResponses;
26+
public bool AnswerSurvey(IEnumerable<SurveyQuestion> questions)
27+
{
28+
if (ConsentToSurvey())
29+
{
30+
surveyResponses = new Dictionary<int, string>();
31+
int index = 0;
32+
foreach (var question in questions)
33+
{
34+
var answer = GenerateAnswer(question);
35+
if (answer != null)
36+
{
37+
surveyResponses.Add(index, answer);
38+
}
39+
index++;
40+
}
41+
}
42+
return surveyResponses != null;
43+
}
44+
45+
private bool ConsentToSurvey() => randomGenerator.Next(0, 2) == 1;
46+
47+
private string? GenerateAnswer(SurveyQuestion question)
48+
{
49+
switch (question.TypeOfQuestion)
50+
{
51+
case QuestionType.YesNo:
52+
int n = randomGenerator.Next(-1, 2);
53+
return (n == -1) ? default : (n == 0) ? "No" : "Yes";
54+
case QuestionType.Number:
55+
n = randomGenerator.Next(-30, 101);
56+
return (n < 0) ? default : n.ToString();
57+
case QuestionType.Text:
58+
default:
59+
switch (randomGenerator.Next(0, 5))
60+
{
61+
case 0:
62+
return default;
63+
case 1:
64+
return "Red";
65+
case 2:
66+
return "Green";
67+
case 3:
68+
return "Blue";
69+
}
70+
return "Red. No, Green. Wait.. Blue... AAARGGGGGHHH!";
71+
}
72+
}
73+
// </SnippetAnswerSurvey>
74+
}
75+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
4+
#nullable enable
5+
namespace NullableIntroduction
6+
{
7+
public class SurveyRun
8+
{
9+
private List<SurveyQuestion> surveyQuestions = new List<SurveyQuestion>();
10+
11+
// <SnippetRunReport>
12+
public IEnumerable<SurveyResponse> AllParticipants => (respondents ?? Enumerable.Empty<SurveyResponse>());
13+
public ICollection<SurveyQuestion> Questions => surveyQuestions;
14+
public SurveyQuestion GetQuestion(int index) => surveyQuestions[index];
15+
// </SnippetRunReport>
16+
17+
public void AddQuestion(QuestionType type, string question) =>
18+
AddQuestion(new SurveyQuestion(type, question));
19+
public void AddQuestion(SurveyQuestion surveyQuestion) => surveyQuestions.Add(surveyQuestion);
20+
21+
// <SnippetPerformSurvey>
22+
private List<SurveyResponse>? respondents;
23+
public void PerformSurvey(int numberOfRespondents)
24+
{
25+
int repondentsConsenting = 0;
26+
respondents = new List<SurveyResponse>();
27+
while (repondentsConsenting < numberOfRespondents)
28+
{
29+
var respondent = SurveyResponse.GetRandomId();
30+
if (respondent.AnswerSurvey(surveyQuestions))
31+
repondentsConsenting++;
32+
respondents.Add(respondent);
33+
}
34+
}
35+
// </SnippetPerformSurvey>
36+
}
37+
}

0 commit comments

Comments
 (0)