Michael Flanakin's Web Log

Comments and complaints on software and technology in general

  Home  |   Contact  |   Syndication    |   Login
  159 Posts | 18 Stories | 95 Comments | 530 Trackbacks

News

This weblog is no longer being maintained. For the latest, check out www.michaelflanakin.com!

Article Categories

Archives

Post Categories

Image Galleries

Miscellaneous

I was reading an article about Whidbey, which I'll be posting about shortly, and I thought about how stupid it is to have both the for and foreach loop structures (I also noticed this post when I logged on to write this). Don't get me wrong, I understand that it's easier to use the foreach loop, but considering the performance issues, why would you? I see VB developers doing it all the time and I just shake my head in partial disgust (maybe that's a tad strong).

Anyway, my issue with this fault is not necessarily with developers, but with Microsoft. Let's think about it. A for loop...

for ( int i=0; i < colComplaints; i++ )
{
  colComplaints[i].Whine();
  ...
}

... and its equivalent foreach loop ...

foreach ( Complaint cmpTemp in colComplaints )
{
  cmpTemp.Whine();
  ...
}

... are very similar. The changes are negligible, but you have to admit that the latter is slightly more readable. And, considering how similar these two approaches are, I imagine the IL can't be too far off (perhaps I will investigate that). Microsoft (sh)could optimize this code at compile-time. Think about it: How hard would it be to convert the for header and replace any instance of the temporary variable with the actual collection/object iterator reference? You can't use the foreach unless an iterator has been identified; and, I'd like to hope that finding the iterator and converting foreach IL code to for loop IL code should be pretty easy, considering some of the advanced things .NET takes on.

This is just an outsider looking in, of course, but hopefully someone at Microsoft will see this possibility and push some sort of change in a future release of their compilers. Considering the [assumed] ease of the compiler modification and the level of freedom it would provide developers (albeit for a simple task), I would have to say that its worth it. Honestly, the only reason I don't use foreach is because of performance.

posted on Tuesday, February 10, 2004 10:00 PM

Feedback

# re: for vs. foreach 2/10/2004 11:15 PM Raymond Chen
Except that they aren't equivalent. The "for" loop will let you change the array inside the loop, whereas "foreach" will throw an InvalidOperationException if you change the array while enumerating it.

"foreach" is a much more general construct than the corresponding "for". In principle, the compiler could be "taught" about how the enumerators for various classes work (array, arraylist, ...) but that would be commingling the compiler with the runtime. And it wouldn't help your custom container class that has its own enumerator - the compiler doesn't know how to convert a "foreach" into a "for" of your custom class.

# re: for vs. foreach 2/12/2004 2:13 AM Jon Rowett
silly question - how do you access the iterator in a foreach loop?

# re: for vs. foreach 3/29/2004 5:08 AM Thiago Auper
Is it possible to use an foreach inside another foreach ??

Thanks in advance,

Thiago

# re: for vs. foreach 3/29/2004 5:10 AM Michael Flanakin
As I mentioned, I don't use the foreach, so I've never tried nested foreach blocks. But, I would expect that this would work with no problems. As long as you don't try to use the same enumerator for both loops. Try it.

# re: for vs. foreach 6/22/2004 2:46 AM cantbelievethatppl
I thought you were joking when I read this. Sadly not I guess.

# re: for vs. foreach 1/2/2007 3:44 AM Mrudula
Its possible to have nested foreach. Check out the following code:

foreach (DataTable dtTab in dsData.Tables)
{
foreach (DataRow dr in dtTab.Rows)
{
foreach (DataColumn dtcol in dtTab.Columns)
{
if (dtcol.DataType == System.Type.GetType("System.Decimal")
|| dtcol.DataType == System.Type.GetType("System.Double"))
{
dr[dtcol] = Math.Round(GeneralFunctions.getIntValue(dr[dtcol]), 2);
}

}
}
}

# re: for vs. foreach 2/7/2008 6:00 AM Natarajan
It is better to go with foreach if your purpose is to access data.

In foreach no need to specify one extra variable and number counts. Also on each iteration in forloop it will check the condition is met or not.

So go with foreach for accessing data.

# re: for vs. foreach 2/16/2008 4:56 AM raja
forwach uses reflection so ur app performance will be slow. use for instead of foereach

Post Feedback

Title:
Name:
Email: (never displayed)
Url:
Comments: 
Please add 1 and 6 and type the answer here: