What is the difference between FOREACH Vs. FOR in C# ? [Resolved]

Posted by Akiii under C# on 2/27/2012 | Points: 10 | Views : 28030 | Status : [Member] | Replies : 12
Hi,
As both foreach and for loops does the same thing. Can anyone tell me what is the difference between these two loops and when to use which loop in a scenario ?

Any help with an example will be helpful....


Thanks and Regards
Akiii




Responses

Posted by: Sksamantaray on: 2/27/2012 [Member] Silver | Points: 50

Up
0
Down

Resolved
Hi,
Refer to this also, a nice article.
http://www.codeproject.com/Articles/6759/FOREACH-Vs-FOR-C

Thanks,
Sanjay

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sushantaguha.Net on: 2/27/2012 [Member] Starter | Points: 25

Up
0
Down
For loop repeatedly execute a statement until a specified execution evaluates to false..

for loop is maintained in condition level... it executed 0 or more times



While for each loop repeats a group of embedded statements for each elements in a array or object collection

For each loop gets rid of the clutter and the opportunity for error by hiding the iterator or index variable completely,comprises less coding then for loop


Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Pradeepkumar417 on: 2/28/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,

Go through this below link

http://www.techbaba.com/q/2560-difference+for+foreach+loop+c%23.aspx

Thanks & Regards,
Software Engineer,
Pradeep Kumar

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Naraayanan on: 2/28/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,
Check this link
http://www.codeproject.com/Articles/6759/FOREACH-Vs-FOR-C

Regards,
Lakshmi Naraayanan.S
http://dotnettechrocks.blogspot.in/
http://abaprocker.blogspot.com/

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akiii on: 2/28/2012 [Member] Bronze | Points: 25

Up
0
Down
Thanks all for the reply,
Some of the links i have already visited. In my opinion, foreach loop takes more time than for loop. And foreach loop is specifically made for iterating over collections(etc)......!

what are you opinion folks ?


Regards
Akiii

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: CGN007 on: 3/5/2012 [Member] Silver | Points: 25

Up
0
Down
+1
Only forward iteration is possible in for-each loop...

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akiii on: 3/5/2012 [Member] Bronze | Points: 25

Up
0
Down
Hi @CGN007,
Can you explain your point a bit more ?


Thanks and Regards
Akiii

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: CGN007 on: 3/6/2012 [Member] Silver | Points: 25

Up
0
Down
Suppose We have an array of 10 elements.
Using for loop we iterate the array in both direction,that is from index 0 to 9 and from 9 to 0.
But using for-each loop,the iteration is possible in forward direction only.

I hope this helps you in some way, even though it's very small info!

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akiii on: 3/6/2012 [Member] Bronze | Points: 25

Up
0
Down
Hi @CGN007,

That's a very good point. Please do share some more points if you happen to have ?


regards
Akiii

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: CGN007 on: 3/6/2012 [Member] Silver | Points: 25

Up
0
Down
@Akiii
Of-course I'll..

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: CGN007 on: 6/29/2012 [Member] Silver | Points: 25

Up
0
Down
Mark as Answer if its helpful to you,that motivates...

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Firemyst on: 5/20/2016 [Member] Starter | Points: 25

Up
0
Down
CHeck out this link to see how they work and their performance across multiple objects such as arrays, Dictionaries, DataRows, etc etc:
http://cc.davelozinski.com/c-sharp/for-vs-foreach-vs-while


Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response