C# for each

Sep

4

2011

What reasons would I want to use a foreach loop over a for loop.

I’ve read, that at best a foreach loop will be just as good, but usually perform less as well as the for loop.

Tags: ,

In: C# Asked By: [3 Grey Star Level]
Answer #1

The only reason I can see to use foreach over for, is that the same bit of code can be written with a little less code than than the for loop.

With that being said, the compiler seems to be doing a little more work in the background than with the for loop.

It maybe worth your while to use a for loop instead of the foreach loop. The few less characters with the foreach loop may not be worth the seconds you saved while writing your code.

Answers Answered By: phil [55 Blue Star Level]

Answer this Question

You must be Logged In to post an Answer.

Not a member yet? Sign Up Now »

Star Points Scale

Earn points for Asking and Answering Questions!

Grey Sta Levelr [1 - 25 Grey Star Level]
Green Star Level [26 - 50 Green Star Level]
Blue Star Level [51 - 500 Blue Star Level]
Orange Star Level [501 - 5000 Orange Star Level]
Red Star Level [5001 - 25000 Red Star Level]
Black Star Level [25001+ Black Star Level]