• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C# NullReferenceException类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了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")
//          ~~~~~
开发者ID:.NET开发者,项目名称:System,代码行数:23,代码来源:NullReferenceException

输出:

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");
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:12,代码来源:NullReferenceException


示例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);   
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:15,代码来源:NullReferenceException

输出:

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);   
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:15,代码来源:NullReferenceException

输出:

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;
}
开发者ID:.NET开发者,项目名称:System,代码行数:34,代码来源:NullReferenceException

输出:

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;
}
开发者ID:.NET开发者,项目名称:System,代码行数:37,代码来源:NullReferenceException

输出:

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;
}
开发者ID:.NET开发者,项目名称:System,代码行数:57,代码来源:NullReferenceException

输出:

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.");
      }   
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:18,代码来源:NullReferenceException

输出:

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();
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:14,代码来源:NullReferenceException

输出:

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();
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:15,代码来源:NullReferenceException

输出:

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;   
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:25,代码来源:NullReferenceException

输出:

Unhandled Exception: System.NullReferenceException: Object reference 
not set to an instance of an object.
at Example.PopulateNames(List`1 names)
at Example.Main()



注:本文中的System.NullReferenceException类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C# ObjectDisposedException类代码示例发布时间:2022-05-24
下一篇:
C# NotSupportedException类代码示例发布时间:2022-05-24
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap