I was working my way through simple tasks on arrays and had this task:
Displays all the elements of an array.
My solution to this problem was to create a variable n that will store Array.Length value. Then I would use it in for-statement. The suggested solution was even more straight forward - it would just use inside the for-statement Array.Length property.
My solution:
int[] array = { 0, 1, 3, 4, 5, 2, 1, -4, -1, 10, 55 };
int n = array.Length;
for (int i = 0; i < n; i++)
{
Console.WriteLine(array[i]);
}
Suggested solution:
int[] array = { 0, 1, 3, 4, 5, 2, 1, -4, -1, 10, 55 };
for(int i = 0; i < array.Length; i++)
{
Console.WriteLine(array[i]);
}
My concern is that if we put Array.Length Property inside a loop statement means that it will re-calculate it n-times (where n = Array.Length). Shouldn't it be bad considering that n can be a very big number?
Is there any difference in terms of processing speed or used storage? Or are they just equal options?