Array initializers

  • Thread starter Thread starter cody
  • Start date Start date
C

cody

Iam wondering why I can do

DateTime[] a = {};

but Iam not allowed to do:

DateTime[ , ] b = { {}, {} };

instead I have to write:

DateTime[ , ]b={{new DateTime()},{new DateTime()}};

this isn't very consistent.
 
cody said:
but Iam not allowed to do:

DateTime[ , ] b = { {}, {} };

You've requested a dimension of the array have 0 elements.
Since arrays are zero-based, indexing one of these dimensions
with an index of 0 would requires it to have an element. Negative
indexes are illegal. Trying to initialize to an empty array is like
this is saying the dimension doesn't exist, so why ask for one?


Derek Harmon
 
but Iam not allowed to do:
DateTime[ , ] b = { {}, {} };

You've requested a dimension of the array have 0 elements.
Since arrays are zero-based, indexing one of these dimensions
with an index of 0 would requires it to have an element. Negative
indexes are illegal. Trying to initialize to an empty array is like
this is saying the dimension doesn't exist, so why ask for one?

You are right. If at least one dimension is zero the array cannot contain
elements so allowing an initializer for that is pointless :)
 
Back
Top