471,349 Members | 1,217 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,349 software developers and data experts.

Initializing object arrays.

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.
Aug 25 '08 #1
8 5127
On Sun, 24 Aug 2008 17:55:46 -0700, Sin Jeong-hun <ty*******@gmail.com>
wrote:
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[i] = 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
Aug 25 '08 #2
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.

On Aug 25, 10:22*am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Sun, 24 Aug 2008 17:55:46 -0700, Sin Jeong-hun <typing...@gmail.com*
wrote:
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[i] = 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
Aug 25 '08 #3
Peter Duniho wrote:
>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öran Andersson
_____
http://www.guffa.com
Aug 25 '08 #4
Sin Jeong-hun wrote:
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.

--
Göran Andersson
_____
http://www.guffa.com
Aug 25 '08 #5
On Sun, 24 Aug 2008 23:50:24 -0700, Göran Andersson <gu***@guffa.com>
wrote:
Peter Duniho wrote:
>>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?
Um. Yes. I guess that's probably not what the OP wanted, is it? At
least that's not what his code showed.

Sorry...I must've still been a little sleepy when I wrote that. :) The
Array.ConvertAll() solution should work fine though. And maybe there's a
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();

That's starting to get a little awkward itself though, and not much
different from the ConvertAll() version.

Oh well...rambling now...

Pete
Aug 25 '08 #6
On Aug 25, 11:28*am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
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();
Aug 26 '08 #7
On Tue, 26 Aug 2008 07:51:14 -0700, Pavel Minaev <in****@gmail.comwrote:
[...]
>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? :)

Pete
Aug 26 '08 #8
On Aug 26, 8:00*pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Tue, 26 Aug 2008 07:51:14 -0700, Pavel Minaev <int...@gmail.comwrote:
[...]
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/Visual...dbackID=363960

Come by and vote! :)
Aug 27 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Mantorok Redgormor | last post: by
10 posts views Thread by Steve | last post: by
12 posts views Thread by jimmij | last post: by
13 posts views Thread by John | last post: by
13 posts views Thread by WaterWalk | last post: by
4 posts views Thread by Peskov Dmitry | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.