what’s the point of using the new operator when initializing an array?

Yes, This is mostly used when you are only declaring the array. For Example you want to take values from user, than you will just declare the array. You will declare the size of the array in the bracket as shown below. In this example, “6” is the size of array.

int[] array1 = new int [6]

Now Let’s takes the value from user and stored it in the array.

for(int i = 0 ; i < array1.length ; i++)
{
array1[i] = int.parse(Console.ReadLine()); 
}

Now, In above Example, you can see that we have run the for loop till length of array. that is 6. array1.length is the property of array that returns the length of the array. int.Parse((Console.ReadLine()) will take the input of the user that will stored it in the ith index of the array.

In short, you can say that we use “new” keyword when we just have to declare the array and don’t have to initialize the values. I hope, it will help you.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top