Multiple array creation with same name in same scope?

In C# : my question is that what will be the memory address of arr, when I am doing arr = new [size] in same scope with varying the size of arr, does the memory expands to new size and starting address
of arr remains same ? or if the new memory allocation happens then the previous data get copied to new memory loacation ?

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]);

    }
}

>Solution :

According to your coding part first you have initialized the array as

arr = new byte[6];

When you again intializing the array as

 arr = new byte[10];

the array size will be allocated to the size of 10.
And also the byte[6] will be collected by the garbage collector.

Leave a Reply