tags:

views:

521

answers:

8

I use this type of construct often.

response = new LocationResponse ();
response.LocationDetails = new LocationDetail[4];
response.LocationDetails[0] = new LocationDetail();
response.LocationDetails[0].site = "ABCDE";
...

The piece I don't fully understand is this piece:

response.LocationDetails[0] = new LocationDetail();

Why does each individual element of the array have to be instantiated?

If you leave it out, you get undefined exceptions.

+11  A: 

Well if an array elements were automatically instantiated, you would be locked into which classes you defined the array with. You wouldn't be able to use arrays of abstract classes or interfaces or any object which doesn't have a default constructor. This is just naming a few problems with automatically instantiating objects in an array.

Kevin
And what if the class doesn't have a default constructor?
Samuel
There really wouldn't be a way to have the object automatically instantiated.
Kevin
+2  A: 

Think of it as a reference pointer, if you don't initialize you have a null value on each of the LocationDetail items.

Otávio Décio
+3  A: 

You don't need to instantiate LocationDetail if it is a value type (struct). You have to instantiate it if it's a class because the default value for a reference type is null.

This code works:

public struct LocationDetail 
{
    private string site;

    public string Site
    {
        get { return site; }
        set { site = value; }
    }
}

static void Main(string[] args)
{
    LocationResponse response = new LocationResponse();
    response.LocationDetails = new LocationDetail[4];
    response.LocationDetails[0].Site = "ABCDE";
    Console.Write(response.LocationDetails[0].Site);
}
Michael Meadows
+2  A: 

It set each element to null, which is the default value for a class until you say otherwise.

How can C# automatically instantiate the elements for you? Perhaps that type in your array doesn't even have a default constructor. Maybe you don't want to consume the memory required by an entire array of elements before you actually have them.

I don't see the advantage to having the runtime try to fill that array in advance.

mquander
+1  A: 

It would be a costly operation, depending on what needs to be done to create the objects. Furthermore, what if the objects cannot simply be instantiated? Or if you need an array of a given type, where only some fields will eventually contain an object and others shoud be null?

Simon Lehmann
+1  A: 

As others have pointed out, the array will be full of nulls after you declare it. You can simplify your initialization logic somewhat by doing this:

response.LocationDetails = new LocationDetails [] {
    new LocationDetails(),
    new LocationDetails(),
    new LocationDetails(),
    new LocationDetails()
};

And what would be even nicer is if the LocationDetails constructor had an overload that took a siteid:

response.LocationDetails = new LocationDetails [] {
    new LocationDetails("ABCDE"),
    new LocationDetails("FGHIJ"),
    new LocationDetails("KLMNO"),
    new LocationDetails("PQRST")
};

Or the super fancy C# 3.0 stuff that Justice points out :)

Sean Bright
+1  A: 
response = new LocationResponse() {
    LocationDetails = new LocationDetail[] {
        new LocationDetail { site = "ABCDE" }
    }
}
Justice
+2  A: 

I think of arrays as egg-cartons. You can declare an egg carton, but that doesn't put eggs in your carton - you still have to decide what kind of egg to put in each slot of the carton. You might want easter eggs in some, but grade AA free-roam brown eggs in others.

The array itself can't know what you want ahead of time, so it defaults to putting nothing in your slots.

Mike