Skip to content

Commit bf22adb

Browse files
authored
add project file and modernize code (#10975)
1 parent 86c9784 commit bf22adb

File tree

14 files changed

+315
-302
lines changed

14 files changed

+315
-302
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
// <Snippet8>
2-
using System;
1+
using System;
32

4-
public class Example
3+
public class Array1Example
54
{
6-
public static void Main()
7-
{
8-
String[] values = { "one", null, "two" };
9-
for (int ctr = 0; ctr <= values.GetUpperBound(0); ctr++)
10-
Console.Write("{0}{1}", values[ctr].Trim(),
11-
ctr == values.GetUpperBound(0) ? "" : ", ");
12-
Console.WriteLine();
13-
}
5+
public static void Main()
6+
{
7+
// <Snippet8>
8+
string[] values = [ "one", null, "two" ];
9+
for (int ctr = 0; ctr <= values.GetUpperBound(0); ctr++)
10+
Console.Write("{0}{1}", values[ctr].Trim(),
11+
ctr == values.GetUpperBound(0) ? "" : ", ");
12+
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>
18+
}
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

+16-15
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
// <Snippet9>
2-
using System;
1+
using System;
32

4-
public class Example
3+
public class Array2Example
54
{
6-
public static void Main()
7-
{
8-
String[] values = { "one", null, "two" };
9-
for (int ctr = 0; ctr <= values.GetUpperBound(0); ctr++)
10-
Console.Write("{0}{1}",
11-
values[ctr] != null ? values[ctr].Trim() : "",
12-
ctr == values.GetUpperBound(0) ? "" : ", ");
13-
Console.WriteLine();
14-
}
5+
public static void Main()
6+
{
7+
// <Snippet9>
8+
string[] values = [ "one", null, "two" ];
9+
for (int ctr = 0; ctr <= values.GetUpperBound(0); ctr++)
10+
Console.Write("{0}{1}",
11+
values[ctr] != null ? values[ctr].Trim() : "",
12+
ctr == values.GetUpperBound(0) ? "" : ", ");
13+
Console.WriteLine();
14+
15+
// The example displays the following output:
16+
// one, , two
17+
// </Snippet9>
18+
}
1519
}
16-
// The example displays the following output:
17-
// one, , two
18-
// </Snippet9>

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

+17-16
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1-
// <Snippet10>
2-
using System;
1+
using System;
32

4-
public class Example
3+
public class Array3Example
54
{
6-
public static void Main()
7-
{
8-
int[] values = null;
9-
for (int ctr = 0; ctr <= 9; ctr++)
10-
values[ctr] = ctr * 2;
5+
public static void Main()
6+
{
7+
// <Snippet10>
8+
int[] values = null;
9+
for (int ctr = 0; ctr <= 9; ctr++)
10+
values[ctr] = ctr * 2;
1111

12-
foreach (var value in values)
13-
Console.WriteLine(value);
14-
}
12+
foreach (int value in values)
13+
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>
20+
}
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

+24-23
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
1-
// <Snippet11>
2-
using System;
1+
using System;
32

4-
public class Example
3+
public class Array4Example
54
{
6-
public static void Main()
7-
{
8-
int[] values = new int[10];
9-
for (int ctr = 0; ctr <= 9; ctr++)
10-
values[ctr] = ctr * 2;
5+
public static void Main()
6+
{
7+
// <Snippet11>
8+
int[] values = new int[10];
9+
for (int ctr = 0; ctr <= 9; ctr++)
10+
values[ctr] = ctr * 2;
1111

12-
foreach (var value in values)
13-
Console.WriteLine(value);
14-
}
12+
foreach (int value in values)
13+
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>
27+
}
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

+65-55
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,72 @@
1-
// <Snippet6>
2-
using System;
1+
using System;
32

4-
public class Example
3+
namespace Chain1
54
{
6-
public static void Main()
7-
{
8-
var pages = new Pages();
9-
if (!String.IsNullOrEmpty(pages.CurrentPage.Title)) {
10-
String title = pages.CurrentPage.Title;
11-
Console.WriteLine("Current title: '{0}'", title);
12-
}
13-
}
14-
}
5+
// <Snippet6>
6+
public class Chain1Example
7+
{
8+
public static void Main()
9+
{
10+
var pages = new Pages();
11+
if (!string.IsNullOrEmpty(pages.CurrentPage.Title))
12+
{
13+
string title = pages.CurrentPage.Title;
14+
Console.WriteLine($"Current title: '{title}'");
15+
}
16+
}
17+
}
1518

16-
public class Pages
17-
{
18-
Page[] page = new Page[10];
19-
int ctr = 0;
19+
public class Pages
20+
{
21+
readonly Page[] _page = new Page[10];
22+
int _ctr = 0;
2023

21-
public Page CurrentPage
22-
{
23-
get { return page[ctr]; }
24-
set {
25-
// Move all the page objects down to accommodate the new one.
26-
if (ctr > page.GetUpperBound(0)) {
27-
for (int ndx = 1; ndx <= page.GetUpperBound(0); ndx++)
28-
page[ndx - 1] = page[ndx];
29-
}
30-
page[ctr] = value;
31-
if (ctr < page.GetUpperBound(0))
32-
ctr++;
33-
}
34-
}
24+
public Page CurrentPage
25+
{
26+
get { return _page[_ctr]; }
27+
set
28+
{
29+
// Move all the page objects down to accommodate the new one.
30+
if (_ctr > _page.GetUpperBound(0))
31+
{
32+
for (int ndx = 1; ndx <= _page.GetUpperBound(0); ndx++)
33+
_page[ndx - 1] = _page[ndx];
34+
}
35+
_page[_ctr] = value;
36+
if (_ctr < _page.GetUpperBound(0))
37+
_ctr++;
38+
}
39+
}
3540

36-
public Page PreviousPage
37-
{
38-
get {
39-
if (ctr == 0) {
40-
if (page[0] == null)
41-
return null;
42-
else
43-
return page[0];
44-
}
45-
else {
46-
ctr--;
47-
return page[ctr + 1];
48-
}
49-
}
50-
}
51-
}
41+
public Page PreviousPage
42+
{
43+
get
44+
{
45+
if (_ctr == 0)
46+
{
47+
if (_page[0] is null)
48+
return null;
49+
else
50+
return _page[0];
51+
}
52+
else
53+
{
54+
_ctr--;
55+
return _page[_ctr + 1];
56+
}
57+
}
58+
}
59+
}
5260

53-
public class Page
54-
{
55-
public Uri URL;
56-
public String Title;
61+
public class Page
62+
{
63+
public Uri URL;
64+
public string Title;
65+
}
66+
67+
// The example displays the following output:
68+
// Unhandled Exception:
69+
// System.NullReferenceException: Object reference not set to an instance of an object.
70+
// at Chain1Example.Main()
71+
// </Snippet6>
5772
}
58-
// The example displays the following output:
59-
// Unhandled Exception:
60-
// System.NullReferenceException: Object reference not set to an instance of an object.
61-
// at Example.Main()
62-
// </Snippet6>

0 commit comments

Comments
 (0)