2

Will the above still create one element?

H H
  • 263,252
  • 30
  • 330
  • 514
user496949
  • 83,087
  • 147
  • 309
  • 426

2 Answers2

11

No.

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

Steven Evers
  • 16,649
  • 19
  • 79
  • 126
9

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.

digEmAll
  • 56,430
  • 9
  • 115
  • 140
  • I don't think so, an array isn't dynamically sizable, so unlike an empty list, you can't add anything to it. – Steven Evers Dec 18 '10 at 09:26
  • 1
    My simile wasn't intended to say that an empty array could be resized/modified, but rather that can be traversed like an empty list or collection... – digEmAll Dec 18 '10 at 09:30
  • I agree with your edit, however the OP asked (in a roundabout way) if any elements could be stored in the array. Clearly, that's not the case. – Steven Evers Dec 18 '10 at 20:39
  • "Will the above still create one element?" english is not my first language, but it sounds to me like: "Does the above code still create an array with one element ?" but I can be wrong... Anyway, It doesn't matter he accepted your answer. :) – digEmAll Dec 19 '10 at 10:38