Fairly basic question, imagine
int a = 5;
int[] b = new int[1];
b[0] = 5;
Do both a
and b
take up the same space in memory? I assume b
is larger than a
as it has to store the length of itself somewhere, so I thought it would be IntPtr.Size
larger, but I am not sure.
I am trying to write code where the length of the array is determined at runtime, and can be 1 or larger (<10). I didn't know if I should just make an array if the length is set to one, or to have a special case in the code and just use the underlying type for length == 1.
I know that a
is a value type while b
is a reference type.
No, a
and b
will not occupy the same amount of memory.
The array container is an object in its own right. It will, somewhere, have to store data pertaining to the number of elements it contains. So it will have a non-zero size.
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