本文整理汇总了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]);
}
}
输出:
'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]);
}
}
输出:
'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];
}
}
输出:
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()
示例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.
示例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));
}
}
输出:
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));
}
}
输出:
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;
}
}
输出:
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;
}
}
输出:
10 6 7 5 8