4

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.

8 Answers 8

13

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.

Sign up to request clarification or add additional context in comments.

2 Comments

And what if the class doesn't have a default constructor?
There really wouldn't be a way to have the object automatically instantiated.
4

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);
}

Comments

2

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

Comments

2

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.

Comments

2

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.

Comments

1

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?

Comments

1

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 :)

Comments

1
response = new LocationResponse() {
    LocationDetails = new LocationDetail[] {
        new LocationDetail { site = "ABCDE" }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.