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 time it repeated.
i’m running dotnet 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 and 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 e resized it at compilation?”
But no, running the code i could see that it was in fact declared with 10 size and after:
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 it something i missing? I tried using gemini to now if it should resize, but it just kept saying that change an array size isn’t possible, wich 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 tough i just assigning a value(in a stupid way i know).
giurno B is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1