C# Array - getting "System.IndexOutOfRangeException" but dont understand why.

JA 0 Reputation points
2025-01-24T08:28:04.7733333+00:00

Hi

namespace ConsoleApp3

{

internal class Program

{

    static void Main(string[] args)

    {

        string[] personNamn = new string[3];

        personNamn[0] = "A";

        personNamn[1] = "B";

        personNamn[2] = "C";

        for (int i = 0; i < 3; i++)

        {

            Console.WriteLine(personNamn[i]);

        }

    }

}

Im trying to learn how to loop arrays. Above is my code and i don't understand why i get "System.IndexOutOfRangeException" concering "Console.WriteLine(personNamn[i];". As i understand this means that the code in the lowest block cant reach the higher ones. Ive tried to write them in the same but get the same message. Does someone understand? Important to stay around this method since im studying for an exam and this will be part. According to the assignments correct answer the solution is:

string[] personNamn = new string[3];
personNamn[0] = "Adam";
personNamn[1] = "Bertil";
personNamn[2] = "Ceasar";

for (int i = 0; i < 3; i++)

{

Console.WriteLine(personNamn[i]); 
```} 

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,339 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Marcin Policht 39,840 Reputation points MVP
    2025-01-24T08:40:25.3433333+00:00

    It looks like you might be getting an error because of a syntax or structural issue with your code, possibly related to how you've written the loop or the code blocks. Based on your description, it seems like you're on the right track but maybe there’s some confusion with how the curly braces {} are being used or maybe an accidental code placement outside of the method scope.

    Original Code:

    string[] personNamn = new string[3];
    personNamn[0] = "Adam";
    personNamn[1] = "Bertil";
    personNamn[2] = "Ceasar";
    
    for (int i = 0; i < 3; i++)
    {
        Console.WriteLine(personNamn[i]);
    }
    

    The error message you're receiving might happen if there is a misplacement of curly braces or if something outside the scope of the loop is incorrectly structured. Double-check if your method or class is correctly formatted around the code.

    1. Try placing the for loop and array declaration are inside a method, like Main()
    using System;
    
    class Program
    {
        static void Main(string[] args)
        {
            string[] personNamn = new string[3];
            personNamn[0] = "Adam";
            personNamn[1] = "Bertil";
            personNamn[2] = "Ceasar";
    
            for (int i = 0; i < 3; i++)
            {
                Console.WriteLine(personNamn[i]);
            }
        }
    }
    
    1. Ensure that each opening { has a corresponding closing }.
    2. The array initialization part you’ve written is fine for assigning values manually. However, if you want to make it more compact, you could also initialize it like this:
    string[] personNamn = { "Adam", "Bertil", "Ceasar" };
    

    If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.

    hth

    Marcin

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.