469,631 Members | 1,225 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,631 developers. It's quick & easy.

is there a foreach performance issue when using complex expressions?

Hi all,

If I have a expensive public property that exposes a collection, say
via a call to a database:

public ArrayList Complex
{
get
{
//expensive call to db
return DbResult;
}
}

and use this in a foreach:

foreach( object o in Complex )
{
...
}

Will the clr call Complex only once, and save a reference to the
direct variable actually exposed, or will it execute the code in the
get routine each iteration?

Thanks in advance,

Ben Joldersma
Sr. Software Engineer,
Citadel Media, Inc.
Nov 15 '05 #1
4 1586
Benjamin Joldersma <be**@theinsiders.com> wrote:

<snip>
Will the clr call Complex only once, and save a reference to the
direct variable actually exposed, or will it execute the code in the
get routine each iteration?


It calls it once, and gets an iterator. So no, you don't need to worry
about the time it takes to evaluate that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
Hi Benjamin,

It call it once.

This is VERY easy to check ( I just did it ), just put a breakpoint inside
the get method , you will see it only enters once.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Benjamin Joldersma" <be**@theinsiders.com> wrote in message
news:8f**************************@posting.google.c om...
Hi all,

If I have a expensive public property that exposes a collection, say
via a call to a database:

public ArrayList Complex
{
get
{
//expensive call to db
return DbResult;
}
}

and use this in a foreach:

foreach( object o in Complex )
{
...
}

Will the clr call Complex only once, and save a reference to the
direct variable actually exposed, or will it execute the code in the
get routine each iteration?

Thanks in advance,

Ben Joldersma
Sr. Software Engineer,
Citadel Media, Inc.

Nov 15 '05 #3
Thanks so much for checking. I should have thought of that!
-----Original Message-----
Hi Benjamin,

It call it once.

This is VERY easy to check ( I just did it ), just put a breakpoint insidethe get method , you will see it only enters once.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Benjamin Joldersma" <be**@theinsiders.com> wrote in message
news:8f**************************@posting.google. com...
Hi all,

If I have a expensive public property that exposes a collection, say via a call to a database:

public ArrayList Complex
{
get
{
//expensive call to db
return DbResult;
}
}

and use this in a foreach:

foreach( object o in Complex )
{
...
}

Will the clr call Complex only once, and save a reference to the direct variable actually exposed, or will it execute the code in the get routine each iteration?

Thanks in advance,

Ben Joldersma
Sr. Software Engineer,
Citadel Media, Inc.

.

Nov 15 '05 #4
-----Original Message-----
Benjamin Joldersma <be**@theinsiders.com> wrote:

<snip>
Will the clr call Complex only once, and save a reference to the direct variable actually exposed, or will it execute the code in the get routine each iteration?
It calls it once, and gets an iterator. So no, you don't

need to worryabout the time it takes to evaluate that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
.

Nov 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Randell D. | last post: by
2 posts views Thread by joerg | last post: by
5 posts views Thread by Scott | last post: by
104 posts views Thread by cody | last post: by
27 posts views Thread by Tripper | last post: by
1 post views Thread by jmacduff | last post: by
14 posts views Thread by jehugaleahsa | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.