Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions snippets/csharp/System/NullReferenceException/Overview/Array1.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
// <Snippet8>
using System;
using System;

public class Example
public class Array1Example
{
public static void Main()
{
String[] values = { "one", null, "two" };
for (int ctr = 0; ctr <= values.GetUpperBound(0); ctr++)
Console.Write("{0}{1}", values[ctr].Trim(),
ctr == values.GetUpperBound(0) ? "" : ", ");
Console.WriteLine();
}
public static void Main()
{
// <Snippet8>
string[] values = [ "one", null, "two" ];
for (int ctr = 0; ctr <= values.GetUpperBound(0); ctr++)
Console.Write("{0}{1}", values[ctr].Trim(),
ctr == values.GetUpperBound(0) ? "" : ", ");
Console.WriteLine();

// The example displays the following output:
// Unhandled Exception:
// System.NullReferenceException: Object reference not set to an instance of an object.
// </Snippet8>
}
}
// The example displays the following output:
// Unhandled Exception:
// System.NullReferenceException: Object reference not set to an instance of an object.
// at Example.Main()
// </Snippet8>
31 changes: 16 additions & 15 deletions snippets/csharp/System/NullReferenceException/Overview/Array2.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
// <Snippet9>
using System;
using System;

public class Example
public class Array2Example
{
public static void Main()
{
String[] values = { "one", null, "two" };
for (int ctr = 0; ctr <= values.GetUpperBound(0); ctr++)
Console.Write("{0}{1}",
values[ctr] != null ? values[ctr].Trim() : "",
ctr == values.GetUpperBound(0) ? "" : ", ");
Console.WriteLine();
}
public static void Main()
{
// <Snippet9>
string[] values = [ "one", null, "two" ];
for (int ctr = 0; ctr <= values.GetUpperBound(0); ctr++)
Console.Write("{0}{1}",
values[ctr] != null ? values[ctr].Trim() : "",
ctr == values.GetUpperBound(0) ? "" : ", ");
Console.WriteLine();

// The example displays the following output:
// one, , two
// </Snippet9>
}
}
// The example displays the following output:
// one, , two
// </Snippet9>
33 changes: 17 additions & 16 deletions snippets/csharp/System/NullReferenceException/Overview/Array3.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
// <Snippet10>
using System;
using System;

public class Example
public class Array3Example
{
public static void Main()
{
int[] values = null;
for (int ctr = 0; ctr <= 9; ctr++)
values[ctr] = ctr * 2;
public static void Main()
{
// <Snippet10>
int[] values = null;
for (int ctr = 0; ctr <= 9; ctr++)
values[ctr] = ctr * 2;

foreach (var value in values)
Console.WriteLine(value);
}
foreach (int value in values)
Console.WriteLine(value);

// The example displays the following output:
// Unhandled Exception:
// System.NullReferenceException: Object reference not set to an instance of an object.
// at Array3Example.Main()
// </Snippet10>
}
}
// The example displays the following output:
// Unhandled Exception:
// System.NullReferenceException: Object reference not set to an instance of an object.
// at Example.Main()
// </Snippet10>
47 changes: 24 additions & 23 deletions snippets/csharp/System/NullReferenceException/Overview/Array4.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
// <Snippet11>
using System;
using System;

public class Example
public class Array4Example
{
public static void Main()
{
int[] values = new int[10];
for (int ctr = 0; ctr <= 9; ctr++)
values[ctr] = ctr * 2;
public static void Main()
{
// <Snippet11>
int[] values = new int[10];
for (int ctr = 0; ctr <= 9; ctr++)
values[ctr] = ctr * 2;

foreach (var value in values)
Console.WriteLine(value);
}
foreach (int value in values)
Console.WriteLine(value);

// The example displays the following output:
// 0
// 2
// 4
// 6
// 8
// 10
// 12
// 14
// 16
// 18
// </Snippet11>
}
}
// The example displays the following output:
// 0
// 2
// 4
// 6
// 8
// 10
// 12
// 14
// 16
// 18
// </Snippet11>
120 changes: 65 additions & 55 deletions snippets/csharp/System/NullReferenceException/Overview/Chain1.cs
Original file line number Diff line number Diff line change
@@ -1,62 +1,72 @@
// <Snippet6>
using System;
using System;

public class Example
namespace Chain1
{
public static void Main()
{
var pages = new Pages();
if (!String.IsNullOrEmpty(pages.CurrentPage.Title)) {
String title = pages.CurrentPage.Title;
Console.WriteLine("Current title: '{0}'", title);
}
}
}
// <Snippet6>
public class Chain1Example
{
public static void Main()
{
var pages = new Pages();
if (!string.IsNullOrEmpty(pages.CurrentPage.Title))
{
string title = pages.CurrentPage.Title;
Console.WriteLine($"Current title: '{title}'");
}
}
}

public class Pages
{
Page[] page = new Page[10];
int ctr = 0;
public class Pages
{
readonly Page[] _page = new Page[10];
int _ctr = 0;

public Page CurrentPage
{
get { return page[ctr]; }
set {
// Move all the page objects down to accommodate the new one.
if (ctr > page.GetUpperBound(0)) {
for (int ndx = 1; ndx <= page.GetUpperBound(0); ndx++)
page[ndx - 1] = page[ndx];
}
page[ctr] = value;
if (ctr < page.GetUpperBound(0))
ctr++;
}
}
public Page CurrentPage
{
get { return _page[_ctr]; }
set
{
// Move all the page objects down to accommodate the new one.
if (_ctr > _page.GetUpperBound(0))
{
for (int ndx = 1; ndx <= _page.GetUpperBound(0); ndx++)
_page[ndx - 1] = _page[ndx];
}
_page[_ctr] = value;
if (_ctr < _page.GetUpperBound(0))
_ctr++;
}
}

public Page PreviousPage
{
get {
if (ctr == 0) {
if (page[0] == null)
return null;
else
return page[0];
}
else {
ctr--;
return page[ctr + 1];
}
}
}
}
public Page PreviousPage
{
get
{
if (_ctr == 0)
{
if (_page[0] is null)
return null;
else
return _page[0];
}
else
{
_ctr--;
return _page[_ctr + 1];
}
}
}
}

public class Page
{
public Uri URL;
public String Title;
public class Page
{
public Uri URL;
public string Title;
}

// The example displays the following output:
// Unhandled Exception:
// System.NullReferenceException: Object reference not set to an instance of an object.
// at Chain1Example.Main()
// </Snippet6>
}
// The example displays the following output:
// Unhandled Exception:
// System.NullReferenceException: Object reference not set to an instance of an object.
// at Example.Main()
// </Snippet6>
Loading