Tilo Jung

Tilo Jung

  • 1.7k
  • 21
  • 4.1k

index was out of array range

May 10 2019 12:39 PM
Hello, I have a problem with the args-array. when I compile the code below - I get the error: index was out of array range, so what am I doing wrong? Thank you.
 
code:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6.   
  7. namespace ConsoleApplication1  
  8. {  
  9.     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             int i;  
  14.             args[0] = "Jurgen01";  
  15.             args[1] = "Jurgen02";  
  16.             //pruefen ob beimProgrammaufruf eine oder mehrere  
  17.             //Zeichenfolgen uebergebenworden sind  
  18.               
  19.             //array args der methode main  
  20.             //error - index was out of array range  
  21.             if (args.Length > 0)  
  22.             {  
  23.                 //die Zeichenfolge an der Konsole anzeigen  
  24.                 for (i = 0; i < args.Length; i++)  
  25.                 Console.WriteLine(args[i]);  
  26.             }  
  27.             else  
  28.             {  
  29.                 Console.WriteLine("Kein Uebergabestring");  
  30.             }  
  31.             Console.ReadLine();  
  32.         }  
  33.     }  

 

Answers (2)