hdjim said:
In this case the square brackets [] mean that you want to have an *array*
of that specified type and the type
your code specifies that you want an array of is BookParticipant. <<
I thought that's what BookParticipant[] bookParticipants does. This
declares an array of type BookParticipant
Yes, it *declares* your intention to set aside some space in memory for a
BookParticipant array, but it does NOT actually create the array in that
space.
So...I'm still not sure I follow. I know BookParticipant[]
bookParticipants is creating an array of BookParticipant objects.
ok.
The new[] is what actually creates (or "instantiates") the array.
and within the braces of the array the code is using the new
keyword to create instances of BookParticipant objects - in this case
2 instances. So I'm still not sure why new[] is used.
I've played with the code with and without new[] preceding the braces
and it seems to be the same.
You've got to understand that there are two steps to being able to use a
type:
1. Set aside some space in memory and declare how you'd like to access that
memory later on.
2. Create an instance of the type and place it into that memory space so it
can be used.
In C#, when you want to declare a variable, you state the name of the
variable and the type it will point to as in:
BookParticipant[] bookParticipants;
This declares a variable of the name "bookParticipants" that will point to a
memory space that will hold an array (because of the square brackets) of
BookParticipant types.
But, with the line as it is, all you have is a variable that points to an
empty memory address that is expecting a BookParticipant array, but hasn't
gotten filled up with one yet. So, you can extend that line to not only
declar the variable, but also instantiate the type as well as in:
BookParticipant[] bookParticipants =
someVariableThatAlreadyPointsToAnArrayOfBookParticipantTypes;
But, what's making this example a bit more confusing is that there isn't
some pre-existing variable that already points to an array of
BookParticipant types, so they are getting made on the fly. This is where
the new[] is coming in, it's being used to indicate that a "new" array (
[] ) is being constructed right here and right now as in:
Object[] x = new[]
{new BookParticipant
{FirstName = "Joe", LastName = "Rattz",
ParticipantType =ParticipantTypes.Author,}
new BookParticipant {FirstName = "Ewan",
LastName ="Buckingham", ParticipantType =
ParticipantTypes.Editor}
Your example is putting this all together to say "I want a variable called
'bookParticipants' that will be a pointer to a array of 'BookParticipant'
types and I'd like to associate that variable with a new instance of an
array that I'm going to make right here and now that is made up of a new
BookParticipant with the following parameters and one more BookParticipant
that is made up of the following parameters."
If you count how many times the word "new" is used in that description,
you'll get 3 of them, which correlates to the 3 uses of the keyword in the
code you've supplied.
It is possible to omit the keyword "new" and still have your code compile,
but you will find that when it runs, you will get an "Object variable not
set to an istance of an object." execption message because your variable
(bookParticipants), wouldn't actually be set to an "instance" of anything.
-Scott