Search code examples
c#arrays.net2d

In C#, how do you declare an array with all values set to zero, without explicitly declaring it?


What I am trying to do is something like this (in c#):

int[,] arr2d = {
                      {0, 0},
                      {0, 0},
                      {0, 0}
                  };

This produces an array that is 2 columns wide and 3 rows in length with all values set to zero.

What I am trying to do is something such that i can initialize an array where I give a value x for width, and a value y for height, which will declare a 2d array of those specifications of which all values are zero.

Essentially, how do I make a 2d array where all values are zero of any height and width WITHOUT declaring each one individually as in the code above.


Solution

  • 0 is a default int value, so you can do the same by this code

    var arr2d = new int[3,2];