user496949
user496949

Reputation: 86155

What does int test[] = new int[0] mean?

Will the above still create one element?

Upvotes: 2

Views: 5387

Answers (2)

digEmAll
digEmAll

Reputation: 57220

It's a simple empty array, just like an empty list or generically an empty collection.

The only difference is that you can't modify it later by adding elements.

It could be really useful for example when a method returns an array as result.
When the method has no results, instead of returning null you can return an empty array, so in the calling code you can skip a useless null check and go directly on the enumeration.

Upvotes: 9

Steven Evers
Steven Evers

Reputation: 17216

No.

You'll get an IndexOutOfRangeException if you try to add anything to that array.

Upvotes: 11

Related Questions