illustrates an attempt to write to a nonexistent array element : Array : Collections Data Structure C# Source Code


Custom Search

C# Source Code » Collections Data Structure » Array »

 

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);
    }

  }

}

           
       
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo Collections Data Structure
» Array