I am trying to understand why I am able to create a C# array without specifying the size at compile time.
C# documentation mentions that you declare an array by specifying its size. In most array initialization examples, I see int[] myArray = new int[5];
Yet, in my example, the size is determined at runtime when the function is invoked.
I understand that a List is recommended when the size is not known.
However, for the sake of learning, I would like to get feedback on why this works.
Im currently on NET 9.0.200
I have already reviewed the below questions, but none provide detail on why this is possible.
Array of an unknown length in C#
Making an array of an unknown size C#
C# Array without a specified size
public int[] CreateArray(int size)
{
int[] numbers = new int[size];
for (int i=0; i<size; i++)
{
numbers[i] = i;
}
return numbers;
}