user2602079
user2602079

Reputation: 1393

MSDN C# Arrays tutorial

I'm learning C# array syntax from the MSDN tutorial. It has this code:

// Array-of-arrays (jagged array)
byte[][] scores = new byte[5][];

// Create the jagged array
for (int i = 0; i < scores.Length; i++)
{
    scores[i] = new byte[i + 3];
}

// Print length of each row
for (int i = 0; i < scores.Length; i++)
{
    Console.WriteLine("Length of row {0} is {1}", i, scores[i].Length);
    Console.ReadLine();
}

And says the output is:

Length of row 0 is 3
Length of row 1 is 4
Length of row 2 is 5
Length of row 3 is 6
Length of row 4 is 7

I have copy pasted the code into the main method of a console application and my console output is:

Length of row 0 is 3

Anyone know why my output is different?

Upvotes: 2

Views: 394

Answers (3)

User2012384
User2012384

Reputation: 4919

        byte[][] scores = new byte[5][];

        // Create the jagged array
        for (int i = 0; i < scores.Length; i++)
        {
            scores[i] = new byte[i + 3];
        }

        // Print length of each row
        for (int i = 0; i < scores.Length; i++)
        {
            Console.WriteLine("Length of row {0} is {1}", i, scores[i].Length);
            Console.ReadLine(); <------ Press enter here to continue, If you want your output like MSDN's, remove this line and the program will output all results
        }

Upvotes: 2

joncarl
joncarl

Reputation: 642

  1. You have a Console.ReadLine() in there, so you have to hit enter before it shows the next line of output.
  2. The Visual Studio (if you are using it) debugger is your friend (or a debugger in general).

Upvotes: 3

dcaswell
dcaswell

Reputation: 3167

Your program is asking you to hit Enter between successive lines of output:

Console.ReadLine();

Upvotes: 5

Related Questions