當前位置: 首頁>>代碼示例>>C#>>正文


C# IndexOutOfRangeException類代碼示例

本文整理匯總了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;未經允許,請勿轉載。