Simple: A foreach loop works on each object in a collection, and provides that object as the appropriate type within the loop:
List<string> list = new List<string>() {"hello", "world"};
foreach (string s in list)
{
Console.WriteLine(s);
}
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:
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]