Mega Code Archive

 
Categories / C# / Collections Data Structure
 

Illustrates an attempt to write to a nonexistent array element

/*   illustrates an attempt to write to a nonexistent array element */ using System; public class Example10_2 {   public static void Main() {     try {       int[] intArray = new int[5];       for (int counter = 0; counter <= intArray.Length; counter++)       {         intArray[counter] = counter;         Console.WriteLine("intArray[" + counter + "] = " + intArray[counter]);       }     }     catch (IndexOutOfRangeException e)     {       Console.WriteLine("IndexOutOfRangeException occurred");       Console.WriteLine("Message = " + e.Message);       Console.WriteLine("Stack trace = " + e.StackTrace);     }   } }