Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// <Snippet8>
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()
{
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:
Expand Down
20 changes: 10 additions & 10 deletions snippets/csharp/System/NullReferenceException/Overview/Array2.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
// <Snippet9>
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()
{
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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
// <Snippet10>
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()
{
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:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
// <Snippet11>
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()
{
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
Expand Down
119 changes: 64 additions & 55 deletions snippets/csharp/System/NullReferenceException/Overview/Chain1.cs
Original file line number Diff line number Diff line change
@@ -1,62 +1,71 @@
// <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: '{0}'", 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] == 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 Example.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>
97 changes: 52 additions & 45 deletions snippets/csharp/System/NullReferenceException/Overview/Chain2.cs
Original file line number Diff line number Diff line change
@@ -1,64 +1,71 @@
// <Snippet7>
using System;

public class Example
public class Chain2Example
{
public static void Main()
{
var pages = new Pages();
Page current = pages.CurrentPage;
if (current != null) {
String title = current.Title;
Console.WriteLine("Current title: '{0}'", title);
}
else {
Console.WriteLine("There is no page information in the cache.");
}
}
public static void Main()
{
var pages = new Pages();
Page current = pages.CurrentPage;
if (current != null)
Comment on lines +9 to +10
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something here seems off if nullable references are enabled. Either current should be a Page? or the null check isn't needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have now explicitly disabled nullable context for the purpose of the examples. Can you review my most recent commit, especially the note I added to the NRE examples in the .xml file?

{
string title = current.Title;
Console.WriteLine("Current title: '{0}'", title);
}
else
{
Console.WriteLine("There is no page information in the cache.");
}
}
}
// The example displays the following output:
// There is no page information in the cache.
// </Snippet7>

public class Pages
{
Page[] page = new Page[10];
int ctr = 0;
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;
public Page PreviousPage
{
get
{
if (_ctr == 0)
{
if (_page[0] == null)
return null;
else
return _page[0];
}
else
return page[0];
}
else {
ctr--;
return page[ctr + 1];
}
}
}
{
_ctr--;
return _page[_ctr + 1];
}
}
}
}

public class Page
{
public Uri URL;
public String Title;
public Uri URL;
public string Title;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Do something
using System;

Console.WriteLine("Hello World");
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
// <Snippet1>
using System;
using System.Collections.Generic;

public class Example
public class UseBeforeAssignExample
{
public static void Main(string[] args)
{
int value = Int32.Parse(args[0]);
List<String> names;
if (value > 0)
names = new List<String>();
public static void Main(string[] args)
{
int value = int.Parse(args[0]);
List<string> names;
if (value > 0)
names = [];

names.Add("Major Major Major");
}
//names.Add("Major Major Major");
}
}
// Compilation displays a warning like the following:
// Example1.cs(10) : warning BC42104: Variable //names// is used before it
Expand Down
Loading