CS0847 error in matrix code in C#

Asked

Viewed 98 times

0

I’m making a simple matrix code in C# to display string values, only it appears CS0847 Error: Matrix initializer of length "3" on line 9 and did not understand what it means.

using System;

namespace TesteMatriz1
{
    class Program
    {
        static void Main(string[] args)
        {
            string[,] lista = new string[3, 3] { { "Dante", "Sparda" }, { "Vergil", "Sparda" }, { "Nero", "Sparda" } };

            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    Console.WriteLine($"Nome: {lista[i, j]}");
                }
            }
            Console.ReadKey();
        }
    }
}

2 answers

3


It means that you are creating a 3 x 3 matrix and you are initiating it with fewer elements, in case very clearly you are doing 3 x 2, if what you want is a matrix like this, then determine the correct size, like this:

using static System.Console;

namespace TesteMatriz1 {
    class Program {
        static void Main(string[] args) {
            var lista = new string[3, 2] { { "Dante", "Sparda" }, { "Vergil", "Sparda" }, { "Nero", "Sparda" } };
            for (int i = 0; i < 3; i++) for (int j = 0; j < 2; j++) WriteLine($"Nome: {lista[i, j]}");
        }
    }
}

Or let the compiler take on the size, like this:

using static System.Console;

namespace TesteMatriz1 {
    class Program {
        static void Main(string[] args) {
            var lista = new string[,] { { "Dante", "Sparda" }, { "Vergil", "Sparda" }, { "Nero", "Sparda" } };
            for (int i = 0; i < 3; i++) for (int j = 0; j < 2; j++) WriteLine($"Nome: {lista[i, j]}");
        }
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

  • Why these two are s together and why you replaced the string list with var list?

  • 'Cause I like to write less code when it makes sense.

  • How can I replace this gambit of Nome ({i},{j}): {lista[i, j]} to show Nome (0,0): Dante, Nome(0,1): Nero, or the way I did is right?

  • If that’s what you want it’s right.

  • How do I join some elements of the matrix in the same line: for example: Dante Sparda is one person and not two, in one line appears Dante and in the other Sparda.

  • Depends exactly how do you want, but that’s another question, try asking, if it doesn’t work open a new question.

  • I get it, I’ll try to do.

Show 2 more comments

1

You defined that the array lista 3x3 but defined it to be only 3x2.
Adding another name to each index would solve your problem. Or you can decrease the setting to new string[3, 2]

I recommend you access a array not with maximum indices hard-coded.
In your case, for example, if the array, by chance, switch to 2x2, you would have problems array out of bounds which would be index out of limit. You’d be trying to access index 3 when it doesn’t even exist.

You can first let the compiler assign a size to your array like this:

string[,] lista = new string[,] { { "Dante", "Sparda" }, { "Vergil", "Sparda" }, { "Nero", "Sparda" } };

And to iterate always reaching the maximum, even if the array changes, like this:

for (int i = 0; i < lista.GetLength(0); i++)
{
    for (int j = 0; j < lista.GetLength(1); j++)
    {
        Console.WriteLine($"Nome: {lista[i, j]}");
    }
}

The .GetLength(int) will take the size of the array. In your case, as it is two-dimensional we will have the GetLength(0) and GetLength(1).

  • 1

    GetLength(int) will help me a lot so I don’t have to keep typing array size.

  • It is possible to put GetLength(int) in an array and in an array where the user must enter values?

  • I didn’t quite understand your statement. I think it might be better to ask another question about it. And why not use Lists?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.