Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
What is Diffrance Between For And For Each loop in C#.net
Posted
Comments
Thanks7872 9-Oct-13 3:22am    
Tons of link on google. Answered many times here also.

Googled?
For Vs Foreach - Benchmark 2013[^]

If you expect answers for your upcoming interviews then it's not right way to learn. You should start to learn C# from start.
Here more than couple of ways to learn anything in internet.
Education Needed[^]
 
Share this answer
 
Comments
Andreas Gieriet 9-Oct-13 3:20am    
My 5!
Cheers
Andi
Simple: A foreach loop works on each object in a collection, and provides that object as the appropriate type within the loop:
C#
List<string> list = new List<string>() {"hello", "world"};
foreach (string s in list)
   {
   Console.WriteLine(s);
   }
// s is not available here.
This is really useful when you have to iterate though each element in a collection, but don't care what index they are at. In contrast, a for loop is more powerful, but harder to read and needs more care. To do the same thing with a for loop would be:
C#
List<string> list = new List<string>() { "hello", "world" };
for (int index = 0; index < list.Count; index++)
    {
    string s = list[index];                
    Console.WriteLine(s);
    }
Generally, if you can use a foreach, you should - but you also have to remember that you cannot alter the collection in any way inside a foreach. If you try, you will get a runtime error. You can alter the collection (adding or removing items for example) within the for loop without any problems.

[edit]Tidied up indentation - OriginalGriff[/edit]
 
Share this answer
 
v2
for Loop: for loop execute the statement or block of statement until the condition is false where it's containing with the length for statement repetition. for loop have three expression.

syntax of for loop: for(Value initialization;condition;increment/decrements)

ex: for(int i=0;i<=5;i++)
{
Console.WriteLine(i);
}



Fooreach loop: In foreeach loop do not need to specify the loop bound value as like minimum or maximum value. Execute a statement for each element of array or group of object.

systex of fooreach loop: foreach(variable declaration in group of object/array name)

ex:
int a = 0;

int[] no = new int[] { 0, 1, 2, 3, 4 };

foreach (int i in no )

{

a = a + i ;

}
 
Share this answer
 
v2
foreach loops through all items in an array (for example)

a for loop has a exact count (like 20 loops)


C#
int[] iarray = {1,2,3,4,5,6,7,8,9,10};

foreach(var item in iarray)
{
 Console.WriteLine(item.ToString());
}


//will give out
1
2
3
4
....
10

C#
int[] iarray = {1,2,3,4,5,6,7,8,9,10};
for(int i = 0; i <=3;i++)
{
 Console.WriteLine(iarray[i].ToString());
}


//will give out
1
2
3
4
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900