Index was outside the bounds of the array
This is a C# Program to demonstrate IndexOutOfRange exception.
This C# Program Demonstrates IndexOutOfRange Exception.
Here if the array has the index value out of the range that is specified then this exception is thrown.
Here is source code of the C# Program to Demonstrate IndexOutOfRange Exception. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.
/* * C# Program to Demonstrate IndexOutOfRange Exception */ using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace differnce { class arrayoutofindex { public void calculatedifference() { int difference=0; int [] number= new int[5] {1,2,3,4,5}; try { for (int init =1; init <=5; init++) { difference= difference - number[init]; } Console.WriteLine("The difference of the array is:" + difference); } catch (IndexOutOfRangeException e) { Console.WriteLine(e.Message); } } } class classmain { static void Main(string [] args) { arrayoutofindex obj = new arrayoutofindex(); obj.calculatedifference(); Console.ReadLine(); } } }
This C# program is used to demonstrate IndexOutOfRange exception. Create an object ‘ob’ variable to arrayoutofindex class, then using object variable perform the calculateddifference() procedure.
In calculatedifference() procedure assign an value to the number[] array variable. In try statement for loop is used to compute the difference between the value of ‘variable’ by the value of ‘number[]’ array variable.
If the array has the index value out of the range that is specified then the exception is thrown. Using try and catch, an error message is displayed when the error occurs.
Index was outside the bounds of the array