new[]

H

hdjim

Hello, the following code comes from a book and I'm not sure what the
new[] preceding the braces is used for. There is no datatype
following the brackets so I'm not sure what it does. Here is the code:

BookParticipant[] bookParticipants = new[] {
new BookParticipant {FirstName = "Joe", LastName = "Rattz",
ParticipantType =
ParticipantTypes.Author},
new BookParticipant {FirstName = "Ewan", LastName =
"Buckingham",
ParticipantType =
ParticipantTypes.Editor}
};

I don't understand why we need the new[] preceding the braces. The
code is creating new objects for each element in the array so what is
the purpose of new[].

I appreciate any explanation. Thanks.
 
S

Scott M.

hdjim said:
Hello, the following code comes from a book and I'm not sure what the
new[] preceding the braces is used for. There is no datatype
following the brackets so I'm not sure what it does. Here is the code:

BookParticipant[] bookParticipants = new[] {
new BookParticipant {FirstName = "Joe", LastName = "Rattz",
ParticipantType =
ParticipantTypes.Author},
new BookParticipant {FirstName = "Ewan", LastName =
"Buckingham",
ParticipantType =
ParticipantTypes.Editor}
};

I don't understand why we need the new[] preceding the braces. The
code is creating new objects for each element in the array so what is
the purpose of new[].

I appreciate any explanation. Thanks.

The keyword "new" is used to tell the .NET Framework to create a *new*
"instance of the type that is specified. 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.

This example is a lttle more complicated in that the array is not only being
"instantiated" with this code, but the underlying array of Strings that the
BookParticipant type requires is also being created on the fly twice.
That's why there are three uses of the "new" keyword

-Scott
 
H

hdjim

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

So...I'm still not sure I follow. I know BookParticipant[]
bookParticipants is creating an array of BookParticipant objects.
ok. 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.
 
S

Scott M.

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
 
P

Peter Duniho

hdjim said:
Hello, the following code comes from a book and I'm not sure what the
new[] preceding the braces is used for. There is no datatype
following the brackets so I'm not sure what it does.

The type is inferred from the left hand side of the assignment.
[...]
I don't understand why we need the new[] preceding the braces.

You don't. For variable initializers, C# allows you to omit the "new",
the type, and the square brackets, and it will implicitly include all of
the necessary parts.

The code could be written as (i.e. without the "new[] "):

BookParticipant[] bookParticipants =
{
new BookParticipant
{
FirstName = "Joe", LastName = "Rattz",
ParticipantType = ParticipantTypes.Author
},
new BookParticipant
{
FirstName = "Ewan", LastName = "Buckingham",
ParticipantType = ParticipantTypes.Editor
}
};

....and it would work just fine.

Pete
 
H

hdjim

exactly. as it did when I was testing it. hence why I was wondering
why the code included the new[]. I wasn't sure why it was there. I
guess it's optional but not required.

thanks
 
G

Göran Andersson

hdjim said:
exactly. as it did when I was testing it. hence why I was wondering
why the code included the new[]. I wasn't sure why it was there.

There are several ways of creating an array. I use an integer array as
example:

The original (C# version 1) creation and population:

int[] x = new int[2];
x[0] = 1;
x[1] = 2;

The new (C# version 3) creation with an initializer:

int[] x = new int[] { 1, 2 };

Creation with the array type incurred from the array data:

int[] x = new[] { 1, 2 };

Creating with the variable type incurred from the array type:

var x = new int[] { 1, 2 };

Creating with the variable type incurred from the array type, which is
incurred from the array data:

var x = new[] { 1, 2 };

(Note that the variable is still strongly typed in all those cases, so
the generated code for those are completely identical, except maybe for
the population method of the first one.)
I guess it's optional but not required.

Hm... That's the same thing... ;)
 
J

Jeff Johnson

There are several ways of creating an array. I use an integer array as
example:

The original (C# version 1) creation and population:

int[] x = new int[2];
x[0] = 1;
x[1] = 2;

The new (C# version 3) creation with an initializer:

int[] x = new int[] { 1, 2 };

I think you meant version 2 for the above, because it works in VS 2005,
which is C# 2.0. Also, there's the shorthand:

int[] x = { 1, 2 };

The rest of your examples are 3.0+.
Creation with the array type incurred from the array data:

int[] x = new[] { 1, 2 };

Creating with the variable type incurred from the array type:

var x = new int[] { 1, 2 };

Creating with the variable type incurred from the array type, which is
incurred from the array data:

var x = new[] { 1, 2 };

(Note that the variable is still strongly typed in all those cases, so the
generated code for those are completely identical, except maybe for the
population method of the first one.)
I guess it's optional but not required.

Hm... That's the same thing... ;)

Heh. I once received a meeting request which stated "mandatory attendence is
optional." Huh?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top