本文整理汇总了C#中System.NullReferenceException类的典型用法代码示例。如果您正苦于以下问题:C# NullReferenceException类的具体用法?C# NullReferenceException怎么用?C# NullReferenceException使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
NullReferenceException类属于System命名空间,在下文中一共展示了NullReferenceException类的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
using System.Collections.Generic;
public class Example
{
public static void Main(string[] args)
{
int value = Int32.Parse(args[0]);
List<String> names;
if (value > 0)
names = new List<String>();
names.Add("Major Major Major");
}
}
// Compilation displays a warning like the following:
// Example1.cs(10) : warning BC42104: Variable //names// is used before it
// has been assigned a value. A null reference exception could result
// at runtime.
//
// names.Add("Major Major Major")
// ~~~~~
输出:
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object. at Example.Main()
示例2: Main
//引入命名空间
using System;
using System.Collections.Generic;
public class Example
{
public static void Main()
{
List<String> names = new List<String>();
names.Add("Major Major Major");
}
}
示例3: Main
//引入命名空间
using System;
public class Example
{
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);
}
}
输出:
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object. at Example.Main()
示例4: Main
//引入命名空间
using System;
public class Example
{
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);
}
}
输出:
0 2 4 6 8 10 12 14 16 18
示例5: Main
//引入命名空间
using System;
public class Example
{
public static void Main()
{
Person[] persons = Person.AddRange( new String[] { "Abigail", "Abra",
"Abraham", "Adrian", "Ariella",
"Arnold", "Aston", "Astor" } );
String nameToFind = "Robert";
Person found = Array.Find(persons, p => p.FirstName == nameToFind);
Console.WriteLine(found.FirstName);
}
}
public class Person
{
public static Person[] AddRange(String[] firstNames)
{
Person[] p = new Person[firstNames.Length];
for (int ctr = 0; ctr < firstNames.Length; ctr++)
p[ctr] = new Person(firstNames[ctr]);
return p;
}
public Person(String firstName)
{
this.FirstName = firstName;
}
public String FirstName;
}
输出:
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object. at Example.Main()
示例6: Main
//引入命名空间
using System;
public class Example
{
public static void Main()
{
Person[] persons = Person.AddRange( new String[] { "Abigail", "Abra",
"Abraham", "Adrian", "Ariella",
"Arnold", "Aston", "Astor" } );
String nameToFind = "Robert";
Person found = Array.Find(persons, p => p.FirstName == nameToFind);
if (found != null)
Console.WriteLine(found.FirstName);
else
Console.WriteLine("{0} not found.", nameToFind);
}
}
public class Person
{
public static Person[] AddRange(String[] firstNames)
{
Person[] p = new Person[firstNames.Length];
for (int ctr = 0; ctr < firstNames.Length; ctr++)
p[ctr] = new Person(firstNames[ctr]);
return p;
}
public Person(String firstName)
{
this.FirstName = firstName;
}
public String FirstName;
}
输出:
Robert not found
示例7: Main
//引入命名空间
using System;
public class Example
{
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 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 class Page
{
public Uri URL;
public String Title;
}
输出:
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object. at Example.Main()
示例8: Main
//引入命名空间
using System;
public class Example
{
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.");
}
}
}
输出:
There is no page information in the cache.
示例9: Main
//引入命名空间
using System;
public class Example
{
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();
}
}
输出:
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object. at Example.Main()
示例10: Main
//引入命名空间
using System;
public class Example
{
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();
}
}
输出:
one, , two
示例11: Main
//引入命名空间
using System;
using System.Collections.Generic;
public class Example
{
public static void Main()
{
List<String> names = GetData();
PopulateNames(names);
}
private static void PopulateNames(List<String> names)
{
String[] arrNames = { "Dakota", "Samuel", "Nikita",
"Koani", "Saya", "Yiska", "Yumaevsky" };
foreach (var arrName in arrNames)
names.Add(arrName);
}
private static List<String> GetData()
{
return null;
}
}
输出:
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object. at Example.PopulateNames(List`1 names) at Example.Main()