C # - Why do you need to instantiate each element of an array?

I often use this type of construction.

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

A piece that I don't quite understand is this thing:

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

Why should every single element of an array have to be created?

If you leave this, you will get undefined exceptions.

+4
source share
8 answers

Well, if the elements of the array were automatically created, you would be locked into which classes you defined the array. You cannot use arrays of abstract classes or interfaces or any object that does not have a default constructor. This just names a few problems with automatically creating objects in an array.

+13
source

You do not need to create an instance of LocationDetail if it is a value type (struct). You must create an instance if it is a class because the default value for the 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); } 
+4
source

Think of it as a link pointer, if you don't initialize, you have a null value for each of the LocationDetail elements.

+2
source

It sets each element to null, which is the default value for the class, unless you say otherwise.

How can C # automatically create items for you? Perhaps this type in your array does not even have a default constructor. You may not want to consume the memory required for the entire array of elements before you actually have them.

I do not see the benefits of having the runtime try to populate this array in advance.

+2
source

I think of cardboard box arrays. You can declare a box with an egg, but that does not put the eggs in your box - you still need to decide which egg to put in each slot in the box. You might want Easter eggs in some, but in other classes, as well as white class AA eggs.

The array itself cannot know what you want ahead of schedule, so by default it does not put anything in your slots.

+2
source

This will be an expensive operation, depending on what needs to be done to create the objects. Also, what if objects cannot just be created? Or, if you need an array of a given type, where only some fields will eventually contain an object and others will be null?

+1
source

As others have pointed out, the array will be filled null after it is declared. You can simplify your initialization logic by doing the following:

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

And what would be even better if the LocationDetails constructor had an overload that took placeid:

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

Or super fancy C # 3.0 stuff referee points out :)

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

All Articles