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

I stumbled upon an unexpected behaviour in C# – Can an array change its size implicitly?

I’m new to programming and just started C#, so just basic exercises. I was trying to just populate an array with random values and getting the biggest number and how many times it repeated.
I’m running .NET 8.0, C# 12.

using static System.Random;

uint[] numberList = new uint[8];
WriteLine(numberList.Length);
/*
for (uint i = 0; i < numberList.Length;i++) {
numberList[i] = (uint)new Random().Next(0,9);
}
*/
numberList = [1,1,1,2,3,8,8,7,10,10,11];
WriteLine(numberList.Length);
uint[] biggerNum = [0,0];

for (uint i = 0; i < numberList.Length; i++){
    if (numberList[i] > biggerNum[0]) {
        biggerNum[0] = numberList[i];
    }
    WriteLine("Value of i: " + i);
    WriteLine("Value in the array: " + numberList[i]);
}

for (uint i = 0; i < numberList.Length; i++) {
    if (numberList[i] == biggerNum[0]) biggerNum[1]++;
}

WriteLine(format:"The number: {0} \nRepeated: {1} times.",
            arg0:biggerNum[0],
            arg1:biggerNum[1]);

While fiddling with it, I declared an array of size 10. But after I populated it with 11 values, it just got resized.

I was expecting to get a compile error, but it just worked. Then I thought "maybe the compiler just recognized that I used 11 values and resized it at compilation?"
But no, running the code I could see that it was in fact declared with size 10 and after

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

numberList = [1,1,1,2,3,8,8,7,10,10,11];

it just got resized to 11. I tried with multiple values, starting it with 8, 5 and then assigning more.
Is there something I’m missing? I tried using Gemini to find out if it should resize, but it just kept saying that changing an array size isn’t possible, which is what I was expecting. Didn’t find this specifically in the documentation.
I know I should declare like uint[] numberList = [1,1,1,2,3,8,8,7,10,10,11]; but this is behaving like I’m shadowing the array even though I’m just assigning a value (in a stupid way I know).

>Solution :

The array was not resized.

You declared an array called numberList. Arrays are reference types, and you made it reference an array with 8 elements. You then assigned an array with more elements, so that numberList now references the larger array.

But the original array was left unchanged. And since nothing references it anymore, it can now be cleaned up 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