What will be the memory address of arr, when I am doing arr = new T[size] twice in the same scope while varying size?
Does the memory expands to new size and starting address
of arr remains the same?
Or if the new memory allocation happens then the previous data get copied to new memory location?
using System;
class HelloWorld
{
static void Main ()
{
byte[] arr;
arr = new byte[6];
arr[2] = 6;
Console.WriteLine ("len = {0}\n{1}", arr.Length, arr[2]);
arr = new byte[10];
arr[2] = 10;
Console.WriteLine ("len = {0}\n{1}", arr.Length, arr[2]);
}
}
new byte[10]allocates a new memory chunk,arris a reference to this chunk;byte[6]is now garbage which can be collected by GC (garbage collector)Console.WriteLine("len = 6\n6"); Console.WriteLine("len = 10\n10")anyway. Asking questions like this could give interesting answers about memory management and compiler optimizations, but what have you tried?arr= someIntArrayOf6Lengthand then doarr = new int[10]and ever get any of the original data in your new 10-long arrayArray.Resize(ref arr, 10);if you wantarrbe created from previousbyte[6]array (previous data will be copied)