Initializing object arrays.

S

Sin Jeong-hun

Is there any way to initialize an array of objects without looping
through each elements?

Class A
{
private int PrivateValue;
public A(int a)
{
PrivateValue=a;
}
}

void Main()
{
A[] arrayA=new A[100];
//At this point, A[0]~A[99] are all nulls
foreach(A a in arrayA) {a=new A(0); }
}

I've been using that kind of code. Is there any precise code? Long
time ago, I hoped
A[] arrayA=new A[100](0);
would work, but it didn't.
 
S

Sin Jeong-hun

Oh yeah, I forgot that I cannot modify the collection inside the
foreach statement. Thank you for all the explations. I'll start
learning LINQ.

But still, I think
A[] arrayA=new A[100](0);
is more intuitive.

Is there any way to initialize an array of objects without looping
through each elements?

That depends.  What version of .NET are you using?  If you have .NET 3.5,  
you can use LINQ:

     A[] arrayA = Enumerable.Repeat(new A(0), 100).ToArray();

Probably less efficient performance-wise than doing it yourself, but it  
does read better.

It might be considered a little awkward, but you can use the  
Array.ConvertAll() method for similar purpose:

     A[] arrayA = Array.ConvertAll(new A[100], delegate { return new A(0);  

});

That's available in .NET 2.0, and maybe almost as efficient as an explicit  
loop (there's an extra throw-away array allocated, but otherwise should be  
about the same).  Not quite as expressive as the LINQ version though.
[...]
void Main()
{
  A[] arrayA=new A[100];
  //At this point, A[0]~A[99] are all nulls
  foreach(A a in arrayA) {a=new A(0); }
}
I've been using that kind of code.

I hope not.  :)  In that code, "a" is read-only and even if you could 
assign to it, it wouldn't change the value in the array, just the variable  
"a".

You probably meant:

     for (int i = 0; i < arrayA.Length; i++)
     {
         arrayA = new A(0);
     }

Hopefully the LINQ version should work for you.  Even if it doesn't, if 
the explicit pattern bothers you, it would be relatively easy for you to  
write a generic method to do the array initialization for you.

Pete
 
G

Göran Andersson

Peter said:
Is there any way to initialize an array of objects without looping
through each elements?

That depends. What version of .NET are you using? If you have .NET
3.5, you can use LINQ:

A[] arrayA = Enumerable.Repeat(new A(0), 100).ToArray();

That will create an array with 100 references to the same object, won't it?
 
G

Göran Andersson

Sin said:
But still, I think
A[] arrayA=new A[100](0);
is more intuitive.

One major reason that there is no such shortcut is probably that it's
usage is very limited. The only thing that you can use it for is to
create an array containing identical objects, which isn't really
something that you do very often.
 
P

Pavel Minaev

Sorry...I must've still been a little sleepy when I wrote that.  :)  The  
Array.ConvertAll() solution should work fine though.  And maybe there'sa  
LINQ approach I'm not thinking of yet.  It's a shame that the Repeat()  
method takes just a single object, and that there's not an overload you  
can pass a delegate to.

Maybe something like:

     A[] arrayA = new A[100].Select(x => new A(0)).ToArray();

Enumerable.Range(0, 100).Select(x => new A(0)).ToArray();
 
P

Pavel Minaev

[...]
Maybe something like:
     A[] arrayA = new A[100].Select(x => new A(0)).ToArray();
Enumerable.Range(0, 100).Select(x => new A(0)).ToArray();

Ah, I was so close.  But I prefer:

     Enumerable.Repeat(0, 100).Select(x => new A(0)).ToArray();

Or even:

     Enumerable.Repeat(0, 100).Select(x => new A(x)).ToArray();

Why make LINQ do any math when it's not necessary?  :)

Still too verbose for my liking, so I've opened a VS Connect ticket
for Enumerable.Generate:

https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=363960

Come by and vote! :)
 

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