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);
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.
Accessing 2D Array Elements Just like 1D arrays, 2D arrays are indexed starting at 0 .
In C++ you can initialize a one dimensional array with 0 with a code like this: int myarray[100] = {0};
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With