Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize double dimensional array with zero in c#

Tags:

arrays

c#

I'm new to c#. I have two dimensional array. I want to initialize with 0.

Here is code. I have an error at Array.fill()

int N = elements;
int M N * 2;
int[,] amn = new int[M,N];
for(int i = 0; i < M; i++)
    Arrays.fill(amn[i], 0);
like image 244
Deepak Avatar asked Oct 03 '13 11:10

Deepak


People also ask

Can you initialize array with 0?

The array will be initialized to 0 in case we provide empty initializer list or just specify 0 in the initializer list. Designated Initializer: This initializer is used when we want to initialize a range with the same value.

Do 2D arrays start at 0?

Accessing 2D Array Elements Just like 1D arrays, 2D arrays are indexed starting at 0 .

How do you initialize a matrix to zero?

In C++ you can initialize a one dimensional array with 0 with a code like this: int myarray[100] = {0};


1 Answers

You don't need to do anything.

From Arrays (C# Programming Guide)

The default values of numeric array elements are set to zero, and reference elements are set to null.

So, when you write;

int[,] amn = new int[M,N];

all elements initalized to 0.

You can see on debugger;

enter image description here

like image 145
Soner Gönül Avatar answered Oct 16 '22 08:10

Soner Gönül