Skip to content

Commit 3a1ca10

Browse files
committed
respond to feedback; add note about nullable context
1 parent cb62b49 commit 3a1ca10

File tree

13 files changed

+120
-139
lines changed

13 files changed

+120
-139
lines changed
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
// <Snippet8>
2-
using System;
1+
using System;
32

43
public class Array1Example
54
{
65
public static void Main()
76
{
7+
// <Snippet8>
88
string[] values = [ "one", null, "two" ];
99
for (int ctr = 0; ctr <= values.GetUpperBound(0); ctr++)
1010
Console.Write("{0}{1}", values[ctr].Trim(),
1111
ctr == values.GetUpperBound(0) ? "" : ", ");
1212
Console.WriteLine();
13+
14+
// The example displays the following output:
15+
// Unhandled Exception:
16+
// System.NullReferenceException: Object reference not set to an instance of an object.
17+
// </Snippet8>
1318
}
1419
}
15-
// The example displays the following output:
16-
// Unhandled Exception:
17-
// System.NullReferenceException: Object reference not set to an instance of an object.
18-
// at Example.Main()
19-
// </Snippet8>

snippets/csharp/System/NullReferenceException/Overview/Array2.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
// <Snippet9>
2-
using System;
1+
using System;
32

43
public class Array2Example
54
{
65
public static void Main()
76
{
7+
// <Snippet9>
88
string[] values = [ "one", null, "two" ];
99
for (int ctr = 0; ctr <= values.GetUpperBound(0); ctr++)
1010
Console.Write("{0}{1}",
1111
values[ctr] != null ? values[ctr].Trim() : "",
1212
ctr == values.GetUpperBound(0) ? "" : ", ");
1313
Console.WriteLine();
14+
15+
// The example displays the following output:
16+
// one, , two
17+
// </Snippet9>
1418
}
1519
}
16-
// The example displays the following output:
17-
// one, , two
18-
// </Snippet9>

snippets/csharp/System/NullReferenceException/Overview/Array3.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1-
// <Snippet10>
2-
using System;
1+
using System;
32

43
public class Array3Example
54
{
65
public static void Main()
76
{
7+
// <Snippet10>
88
int[] values = null;
99
for (int ctr = 0; ctr <= 9; ctr++)
1010
values[ctr] = ctr * 2;
1111

1212
foreach (int value in values)
1313
Console.WriteLine(value);
14+
15+
// The example displays the following output:
16+
// Unhandled Exception:
17+
// System.NullReferenceException: Object reference not set to an instance of an object.
18+
// at Array3Example.Main()
19+
// </Snippet10>
1420
}
1521
}
16-
// The example displays the following output:
17-
// Unhandled Exception:
18-
// System.NullReferenceException: Object reference not set to an instance of an object.
19-
// at Example.Main()
20-
// </Snippet10>

snippets/csharp/System/NullReferenceException/Overview/Array4.cs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
1-
// <Snippet11>
2-
using System;
1+
using System;
32

43
public class Array4Example
54
{
65
public static void Main()
76
{
7+
// <Snippet11>
88
int[] values = new int[10];
99
for (int ctr = 0; ctr <= 9; ctr++)
1010
values[ctr] = ctr * 2;
1111

1212
foreach (int value in values)
1313
Console.WriteLine(value);
14+
15+
// The example displays the following output:
16+
// 0
17+
// 2
18+
// 4
19+
// 6
20+
// 8
21+
// 10
22+
// 12
23+
// 14
24+
// 16
25+
// 18
26+
// </Snippet11>
1427
}
1528
}
16-
// The example displays the following output:
17-
// 0
18-
// 2
19-
// 4
20-
// 6
21-
// 8
22-
// 10
23-
// 12
24-
// 14
25-
// 16
26-
// 18
27-
// </Snippet11>

snippets/csharp/System/NullReferenceException/Overview/Chain1.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,10 @@ public class Page
6363
public Uri URL;
6464
public string Title;
6565
}
66+
6667
// The example displays the following output:
6768
// Unhandled Exception:
6869
// System.NullReferenceException: Object reference not set to an instance of an object.
69-
// at Example.Main()
70+
// at Chain1Example.Main()
7071
// </Snippet6>
7172
}

snippets/csharp/System/NullReferenceException/Overview/Chain2.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
// <Snippet7>
2-
using System;
1+
using System;
32

43
public class Chain2Example
54
{
65
public static void Main()
76
{
7+
// <Snippet7>
88
var pages = new Pages();
99
Page current = pages.CurrentPage;
1010
if (current != null)
@@ -16,11 +16,12 @@ public static void Main()
1616
{
1717
Console.WriteLine("There is no page information in the cache.");
1818
}
19+
20+
// The example displays the following output:
21+
// There is no page information in the cache.
22+
// </Snippet7>
1923
}
2024
}
21-
// The example displays the following output:
22-
// There is no page information in the cache.
23-
// </Snippet7>
2425

2526
public class Pages
2627
{
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
1-
// Do something
2-
using System;
3-
4-
Console.WriteLine("Hello World");
1+
//NullReturnExample.NoCheckExample();
2+
NullReturnExample.ExampleWithNullCheck();

snippets/csharp/System/NullReferenceException/Overview/Project.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5+
<Nullable>disable</Nullable>
56
<TargetFramework>net9.0</TargetFramework>
67
</PropertyGroup>
78

snippets/csharp/System/NullReferenceException/Overview/example1.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ public static void Main(string[] args)
1313
//names.Add("Major Major Major");
1414
}
1515
}
16+
1617
// Compilation displays a warning like the following:
17-
// Example1.cs(10) : warning BC42104: Variable //names// is used before it
18+
// warning BC42104: Variable //names// is used before it
1819
// has been assigned a value. A null reference exception could result
1920
// at runtime.
2021
//
@@ -23,5 +24,5 @@ public static void Main(string[] args)
2324
// The example displays output like the following output:
2425
// Unhandled Exception: System.NullReferenceException: Object reference
2526
// not set to an instance of an object.
26-
// at Example.Main()
27+
// at UseBeforeAssignExample.Main()
2728
// </Snippet1>

snippets/csharp/System/NullReferenceException/Overview/example2.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// <Snippet3>
2-
using System;
32
using System.Collections.Generic;
43

54
public class NRE2Example
@@ -23,9 +22,10 @@ private static List<string> GetData()
2322
return null;
2423
}
2524
}
25+
2626
// The example displays output like the following:
2727
// Unhandled Exception: System.NullReferenceException: Object reference
2828
// not set to an instance of an object.
29-
// at Example.PopulateNames(List`1 names)
30-
// at Example.Main()
29+
// at NRE2Example.PopulateNames(List`1 names)
30+
// at NRE2Example.Main()
3131
// </Snippet3>

0 commit comments

Comments
 (0)