By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,154 Members | 1,405 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,154 IT Pros & Developers. It's quick & easy.

Efficiency of foreach vs for loop

P: n/a
When dealing with reference types, is there an efficiency advantage in using
a for loop instead of a foreach? I though I read somewhere that when using
value types a for loop was more efficient because of the boxing involved
when using foreach.

Thanks,
Jim
Jan 30 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Hi,

"Jim H" <ji**@nospam.nospamwrote in message
news:uV**************@TK2MSFTNGP02.phx.gbl...
| When dealing with reference types, is there an efficiency advantage in
using
| a for loop instead of a foreach? I though I read somewhere that when
using
| value types a for loop was more efficient because of the boxing involved
| when using foreach.

This has been discussed here ad-nauseam, look in the archives for those
thread.

The conclusion is that you should your time focused in more important
matters :)

I do not see how the boxing can be avoided with either solution though.
--
Ignacio Machin
machin AT laceupsolutions com
Jan 30 '07 #2

P: n/a
Jim H <ji**@nospam.nospamwrote:
When dealing with reference types, is there an efficiency advantage in using
a for loop instead of a foreach? I though I read somewhere that when using
value types a for loop was more efficient because of the boxing involved
when using foreach.
It depends on *exactly* which collection you're looking through.
However, usually the difference is so small, you should use whichever
you find more readable. (IMO that's usually foreach.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 30 '07 #3

P: n/a
Thanks to both of you (John and Ignacio).

I'll just use whatever looks easier to read for others in each situation.

jim
"Ignacio Machin ( .NET/ C# MVP )" <machin TA laceupsolutions.comwrote in
message news:%2****************@TK2MSFTNGP04.phx.gbl...
Hi,

"Jim H" <ji**@nospam.nospamwrote in message
news:uV**************@TK2MSFTNGP02.phx.gbl...
| When dealing with reference types, is there an efficiency advantage in
using
| a for loop instead of a foreach? I though I read somewhere that when
using
| value types a for loop was more efficient because of the boxing involved
| when using foreach.

This has been discussed here ad-nauseam, look in the archives for those
thread.

The conclusion is that you should your time focused in more important
matters :)

I do not see how the boxing can be avoided with either solution though.
--
Ignacio Machin
machin AT laceupsolutions com


Jan 30 '07 #4

P: n/a
On Jan 30, 12:33 pm, "Jim H" <j...@nospam.nospamwrote:
When dealing with reference types, is there an efficiency advantage in using
a for loop instead of a foreach? I though I read somewhere that when using
value types a for loop was more efficient because of the boxing involved
when using foreach.
There are situations in which "for" is definitely worse, as well. For
example, if you have a property that builds and returns an aggregate
structure such as an array:

public MyThing[] MyThings
{
get { return
(MyThing[])this._internalMyThings.ToArray(typeof(MyThing)); }
}

(Yes, I know that this is bad practice, and that FxCop specifically
warns against it, but say you had a property like this....)

Then this

for (int x = 0; x < myObj.MyThings.Length; x++)
{
MyThing thing = myObj.MyThings[i];
}

gives absolutlely horrible performance, while this:

foreach (MyThing thing in myObj.MyThings)
{
}

gives good performance.

Again, it all depends on what you're doing, and in the end it's
usually bad designs that kill performance, not using one style of loop
versus another.

Jan 30 '07 #5

P: n/a
Hi Jim,

Yes, I also agree with others that we should pay more attention to other
more performance concern places. However, if your application manipulates
string very often, the "for" may improve some performance, please search
"Use For Loops for String Iteration" section in the link below:
"Performance Tips and Tricks in .NET Applications"
http://msdn2.microsoft.com/en-us/library/ms973839.aspx

Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Jan 31 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.