I am still struggling with understanding the creation of objects.
For example.
int[] x = {4,5,6};
int[] y = new int[x.Length];

Why is it that for the first array, we can initialize it without the use of the new keyword yet in the second line, we need the new keyword?
Whats the difference?
Thanks so much.

Recommended Answers

All 3 Replies

Member Avatar for iamthwee

Do you have a book to refer back to.

I think I got it. One creates an instance and fills it.
The other creates it but leaves it empty and with a size. I guess if we are just going to create it, we need the extra step but if we initialize it and fill it, we can short circuit it.

Yep. Thats right. Its easy to initialize the array. It is sometimes a pain in the butt to fill it with objects though. You have to create a loop to intialize the individual values (or use the brackets for simple data types.) But you know all this now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.