webdad3
webdad3

Reputation: 9080

load string array dynamically at runtime

I currently want a 2D array (4,4) to look like this:

        outArr = new string[4, 4]
        {
            {"0","0","0","0" },
            {"0","0","0","0" },
            {"0","0","0","0" },
            {"0","0","0","0" }
        };

However, I'm unsure of how to do with in code where the array sizes could be dynamic at runtime (i.e. 3,5 or 10,10)

I found this example on how to create the array dynamically (for an int array):

        int[,] myArray=new int[(int)s[0],(int)s[2]];
        myArray[0, 0] = 2;
        Console.WriteLine(myArray[0, 0]);

        Console.ReadLine();

But I want to know how to create the elements "0" in my array dynamically.

Upvotes: 0

Views: 43

Answers (1)

Enigmativity
Enigmativity

Reputation: 117027

Try this:

var r = 7;
var c = 4;

var outArr = new string[r, c];

for (var i = 0; i < r; i++)
    for (var j = 0; j < c; j++)
        outArr[i, j] = "0";

That gives me:

outArr

Upvotes: 1

Related Questions