Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading