Skip to content

Commit 1f36747

Browse files
committed
add project file and modernize code
1 parent c1fa93c commit 1f36747

File tree

14 files changed

+266
-234
lines changed

14 files changed

+266
-234
lines changed

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

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
// <Snippet8>
22
using System;
33

4-
public class Example
4+
public class Array1Example
55
{
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-
}
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+
}
1414
}
1515
// The example displays the following output:
1616
// Unhandled Exception:

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

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
// <Snippet9>
22
using System;
33

4-
public class Example
4+
public class Array2Example
55
{
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-
}
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+
}
1515
}
1616
// The example displays the following output:
1717
// one, , two

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

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
// <Snippet10>
22
using System;
33

4-
public class Example
4+
public class Array3Example
55
{
6-
public static void Main()
7-
{
8-
int[] values = null;
9-
for (int ctr = 0; ctr <= 9; ctr++)
10-
values[ctr] = ctr * 2;
6+
public static void Main()
7+
{
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+
}
1515
}
1616
// The example displays the following output:
1717
// Unhandled Exception:

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

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
// <Snippet11>
22
using System;
33

4-
public class Example
4+
public class Array4Example
55
{
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;
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;
1111

12-
foreach (var value in values)
13-
Console.WriteLine(value);
14-
}
12+
foreach (int value in values)
13+
Console.WriteLine(value);
14+
}
1515
}
1616
// The example displays the following output:
1717
// 0

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

+64-55
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,71 @@
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: '{0}'", 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] == 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+
// The example displays the following output:
67+
// Unhandled Exception:
68+
// System.NullReferenceException: Object reference not set to an instance of an object.
69+
// at Example.Main()
70+
// </Snippet6>
5771
}
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>
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,71 @@
11
// <Snippet7>
22
using System;
33

4-
public class Example
4+
public class Chain2Example
55
{
6-
public static void Main()
7-
{
8-
var pages = new Pages();
9-
Page current = pages.CurrentPage;
10-
if (current != null) {
11-
String title = current.Title;
12-
Console.WriteLine("Current title: '{0}'", title);
13-
}
14-
else {
15-
Console.WriteLine("There is no page information in the cache.");
16-
}
17-
}
6+
public static void Main()
7+
{
8+
var pages = new Pages();
9+
Page current = pages.CurrentPage;
10+
if (current != null)
11+
{
12+
string title = current.Title;
13+
Console.WriteLine("Current title: '{0}'", title);
14+
}
15+
else
16+
{
17+
Console.WriteLine("There is no page information in the cache.");
18+
}
19+
}
1820
}
1921
// The example displays the following output:
2022
// There is no page information in the cache.
2123
// </Snippet7>
2224

2325
public class Pages
2426
{
25-
Page[] page = new Page[10];
26-
int ctr = 0;
27+
readonly Page[] _page = new Page[10];
28+
int _ctr = 0;
2729

28-
public Page CurrentPage
29-
{
30-
get { return page[ctr]; }
31-
set {
32-
// Move all the page objects down to accommodate the new one.
33-
if (ctr > page.GetUpperBound(0)) {
34-
for (int ndx = 1; ndx <= page.GetUpperBound(0); ndx++)
35-
page[ndx - 1] = page[ndx];
36-
}
37-
page[ctr] = value;
38-
if (ctr < page.GetUpperBound(0))
39-
ctr++;
40-
}
41-
}
30+
public Page CurrentPage
31+
{
32+
get { return _page[_ctr]; }
33+
set
34+
{
35+
// Move all the page objects down to accommodate the new one.
36+
if (_ctr > _page.GetUpperBound(0))
37+
{
38+
for (int ndx = 1; ndx <= _page.GetUpperBound(0); ndx++)
39+
_page[ndx - 1] = _page[ndx];
40+
}
41+
_page[_ctr] = value;
42+
if (_ctr < _page.GetUpperBound(0))
43+
_ctr++;
44+
}
45+
}
4246

43-
public Page PreviousPage
44-
{
45-
get {
46-
if (ctr == 0) {
47-
if (page[0] == null)
48-
return null;
47+
public Page PreviousPage
48+
{
49+
get
50+
{
51+
if (_ctr == 0)
52+
{
53+
if (_page[0] == null)
54+
return null;
55+
else
56+
return _page[0];
57+
}
4958
else
50-
return page[0];
51-
}
52-
else {
53-
ctr--;
54-
return page[ctr + 1];
55-
}
56-
}
57-
}
59+
{
60+
_ctr--;
61+
return _page[_ctr + 1];
62+
}
63+
}
64+
}
5865
}
5966

6067
public class Page
6168
{
62-
public Uri URL;
63-
public String Title;
69+
public Uri URL;
70+
public string Title;
6471
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Do something
2+
using System;
3+
4+
Console.WriteLine("Hello World");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>

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

+9-10
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
// <Snippet1>
2-
using System;
32
using System.Collections.Generic;
43

5-
public class Example
4+
public class UseBeforeAssignExample
65
{
7-
public static void Main(string[] args)
8-
{
9-
int value = Int32.Parse(args[0]);
10-
List<String> names;
11-
if (value > 0)
12-
names = new List<String>();
6+
public static void Main(string[] args)
7+
{
8+
int value = int.Parse(args[0]);
9+
List<string> names;
10+
if (value > 0)
11+
names = [];
1312

14-
names.Add("Major Major Major");
15-
}
13+
//names.Add("Major Major Major");
14+
}
1615
}
1716
// Compilation displays a warning like the following:
1817
// Example1.cs(10) : warning BC42104: Variable //names// is used before it

0 commit comments

Comments
 (0)