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

C# IndexOutOfRangeException类代码示例

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

本文整理汇总了C#中System.IndexOutOfRangeException的典型用法代码示例。如果您正苦于以下问题:C# IndexOutOfRangeException类的具体用法?C# IndexOutOfRangeException怎么用?C# IndexOutOfRangeException使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



IndexOutOfRangeException类属于System命名空间,在下文中一共展示了IndexOutOfRangeException类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。

示例1: Main

//引入命名空间
using System;
using System.Collections.Generic;

public class Example
{
   public static void Main()
   {
      List<Char> characters = new List<Char>();
      characters.InsertRange(0, new Char[] { 'a', 'b', 'c', 'd', 'e', 'f' } );
      for (int ctr = 0; ctr <= characters.Count; ctr++)
         Console.Write("'{0}'    ", characters[ctr]);
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:14,代码来源:IndexOutOfRangeException

输出:

'a'    'b'    'c'    'd'    'e'    'f'
Unhandled Exception: 
System.ArgumentOutOfRangeException: 
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
at Example.Main()


示例2: Main

//引入命名空间
using System;
using System.Collections.Generic;

public class Example
{
   public static void Main()
   {
      List<Char> characters = new List<Char>();
      characters.InsertRange(0, new Char[] { 'a', 'b', 'c', 'd', 'e', 'f' } );
      for (int ctr = 0; ctr < characters.Count; ctr++)
         Console.Write("'{0}'    ", characters[ctr]);
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:14,代码来源:IndexOutOfRangeException

输出:

'a'    'b'    'c'    'd'    'e'    'f'


示例3: Main

public class Example
{
   public static void Main()
   {
      int[] values1 = { 3, 6, 9, 12, 15, 18, 21 };
      int[] values2 = new int[6];
      
      // Assign last element of the array to the new array.
      values2[values1.Length - 1] = values1[values1.Length - 1];
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:11,代码来源:IndexOutOfRangeException

输出:

Unhandled Exception: 
System.IndexOutOfRangeException: 
Index was outside the bounds of the array.
at Example.Main()


示例4: Main

//引入命名空间
using System;
using System.Collections.Generic;

public class Example
{
   static List<int> numbers = new List<int>();

   public static void Main()
   {
      int startValue; 
      string[] args = Environment.GetCommandLineArgs();
      if (args.Length < 2) 
         startValue = 2;
      else 
         if (! Int32.TryParse(args[1], out startValue))
            startValue = 2;

      ShowValues(startValue);
   }
   
   private static void ShowValues(int startValue)
   {   
      // Create a collection with numeric values.
      if (numbers.Count == 0)  
         numbers.AddRange( new int[] { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22} );

      // Get the index of a startValue.
      Console.WriteLine("Displaying values greater than or equal to {0}:",
                        startValue);
      int startIndex = numbers.IndexOf(startValue);
      // Display all numbers from startIndex on.
      for (int ctr = startIndex; ctr < numbers.Count; ctr++)
         Console.Write("    {0}", numbers[ctr]);
   }
}
// The example displays the following output if the user supplies
// 7 as a command-line parameter:
//    Displaying values greater than or equal to 7:
//    
//    Unhandled Exception: System.ArgumentOutOfRangeException: 
//    Index was out of range. Must be non-negative and less than the size of the collection.
//    Parameter name: index
//       at System.Collections.Generic.List`1.get_Item(Int32 index)
//       at Example.ShowValues(Int32 startValue)
//       at Example.Main()
开发者ID:.NET开发者,项目名称:System,代码行数:46,代码来源:IndexOutOfRangeException


示例5: Main

//引入命名空间
using System;
using System.Collections.Generic;

public class Example
{
   static List<int> numbers = new List<int>();

   public static void Main()
   {
      int startValue; 
      string[] args = Environment.GetCommandLineArgs();
      if (args.Length < 2) 
         startValue = 2;
      else 
         if (! Int32.TryParse(args[1], out startValue))
            startValue = 2;

      ShowValues(startValue);
   }
   
   private static void ShowValues(int startValue)
   {   
      // Create a collection with numeric values.
      if (numbers.Count == 0)  
         numbers.AddRange( new int[] { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22} );

      // Get the index of startValue.
      int startIndex = numbers.IndexOf(startValue);
      if (startIndex < 0) {
         Console.WriteLine("Unable to find {0} in the collection.", startValue);
      }
      else {
         // Display all numbers from startIndex on.
         Console.WriteLine("Displaying values greater than or equal to {0}:",
                        startValue);
         for (int ctr = startIndex; ctr < numbers.Count; ctr++)
            Console.Write("    {0}", numbers[ctr]);
      }
   }
}
// The example displays the following output if the user supplies
// 7 as a command-line parameter:
//      Unable to find 7 in the collection.
开发者ID:.NET开发者,项目名称:System,代码行数:44,代码来源:IndexOutOfRangeException


示例6: Main

//引入命名空间
using System;

public class Example
{
   public static void Main()
   {
      Array values = Array.CreateInstance(typeof(int), new int[] { 10 }, 
                                          new int[] { 1 });
      int value = 2;
      // Assign values.
      for (int ctr = 0; ctr < values.Length; ctr++) {
         values.SetValue(value, ctr);
         value *= 2;
      }
      
      // Display values.
      for (int ctr = 0; ctr < values.Length; ctr++)
         Console.Write("{0}    ", values.GetValue(ctr));
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:21,代码来源:IndexOutOfRangeException

输出:

Unhandled Exception: 
System.IndexOutOfRangeException: Index was outside the bounds of the array.
at System.Array.InternalGetReference(Void* elemRef, Int32 rank, Int32* pIndices)
at System.Array.SetValue(Object value, Int32 index)
at Example.Main()


示例7: Main

//引入命名空间
using System;

public class Example
{
   public static void Main()
   {
      Array values = Array.CreateInstance(typeof(int), new int[] { 10 }, 
                                          new int[] { 1 });
      int value = 2;
      // Assign values.
      for (int ctr = values.GetLowerBound(0); ctr <= values.GetUpperBound(0); ctr++) {
         values.SetValue(value, ctr);
         value *= 2;
      }
      
      // Display values.
      for (int ctr = values.GetLowerBound(0); ctr <= values.GetUpperBound(0); ctr++)
         Console.Write("{0}    ", values.GetValue(ctr));
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:21,代码来源:IndexOutOfRangeException

输出:

2    4    8    16    32    64    128    256    512    1024


示例8: Main

//引入命名空间
using System;

public class Example
{
   public static void Main()
   {
      // Generate array of random values.
      int[] values = PopulateArray(5, 10);
      // Display each element in the array.
      foreach (var value in values)
         Console.Write("{0}   ", values[value]);
   }

   private static int[] PopulateArray(int items, int maxValue)
   {
      int[] values = new int[items];
      Random rnd = new Random();
      for (int ctr = 0; ctr < items; ctr++)
         values[ctr] = rnd.Next(0, maxValue + 1);   

      return values;                                                      
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:24,代码来源:IndexOutOfRangeException

输出:

6   4   4
Unhandled Exception: System.IndexOutOfRangeException: 
Index was outside the bounds of the array.
at Example.Main()


示例9: Main

//引入命名空间
using System;

public class Example
{
   public static void Main()
   {
      // Generate array of random values.
      int[] values = PopulateArray(5, 10);
      // Display each element in the array.
      foreach (var value in values)
         Console.Write("{0}   ", value);
   }

   private static int[] PopulateArray(int items, int maxValue)
   {
      int[] values = new int[items];
      Random rnd = new Random();
      for (int ctr = 0; ctr < items; ctr++)
         values[ctr] = rnd.Next(0, maxValue + 1);   

      return values;                                                      
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:24,代码来源:IndexOutOfRangeException

输出:

10   6   7   5   8



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


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
C# InvalidCastException类代码示例发布时间:2022-05-24
下一篇:
C# SecurityToken类代码示例发布时间: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