tags:

views:

154

answers:

5

How do I initialize this array of custom types:

PostType[] q = new PostType[qArray.Length];

//initialize array
for( int x = 0; x < qArray.Length; x++)
    q[x] = new PostType();

Is there a better way to initialize this array?

+1  A: 

There is actually no better way to do this. Refer to http://dotnetperls.com/initialize-array
There are several other methods, but simple loop is significally faster

alemjerus
+2  A: 

There's no better way if PostType is a class: you need to visit each position in the array and set it.

If PostType is a struct, then you don't need to do anything at all: each position in the array is automatically initialised to the default value of the struct (all fields 0). (But if you wanted to use a nondefault constructor then you'd be back to the same position as with classes -- you'd need to visit each position and explicitly call the nondefault constructor.)

itowlson
A: 

Not that I can think off, when you create the array memory is located for it, other than that every index points to null. The best way to make your code more efficient would be to store qArray.Length into a variable so you don't call .length on every iteration of loop i.e.

int a = qLength
    for( int x = 0; x < qLength; x++)
        q[x] = new PostType();

Note: Enumerable Repeat will not work as it creates one instance of the object and then repeats a reference to this object.

Aly
+1  A: 

The way you are doing it is fine:

PostType[] q = new PostType[qArray.Length];
for (int i = 0; i < q.Length; i++)
    q[i] = new PostType();

One thing I have changed are to rename the index veriable from x to i, as I find this easier to read, although it's a subjective thing.

Another thing I have changed is the for loop end condition should depend on the length of q, not on the length of qArray. The reason for this is that with your method if you decide to change the first line to use a different length instead of qArray.Length, you'd have to remember to change the second line too. With the modified code you only need to update the first line of code and the rest will work without modification.

You could also do this using Linq:

PostType[] q = Enumerable.Range(0, qArray.Length)
                         .Select(_ => new PostType())
                         .ToArray();

But for large arrays this will be slower and not really easier to read in my opinion (especially if you haven't seen it before). I think I'd probably just stick with the first method if I were you.

Mark Byers
Enumerable Repeat will not work as it repeats a reference to the first object instead of creating new ones
Aly
Enumerable.Range works (it generates a new object for each call). But I'd advise against either Repeat or Range - both for readability and performance. I've updated my comment with an explicit example and an explanation of why I think you should not use it.
Mark Byers
+1  A: 

Piece of cake. Why not do something like this. It's concise.

PostType[] q = qArray.Select(i => new PostType()).ToArray();
Dylan Vester