Interviews3 answers
Major and importance difference between for and foreach loop ?
3 Answers
yes
Venkatesh KottePosted Mar 10, 2015, 1:21 AM EDT
For Loop
static int Method1(int[] array)
{
int a = 0;
for (int i = 0; i < array.Length; i++)
{
a += array[i];
}
return a;
}
Foreach loop
static int Method2(int[] array)
{
int a = 0;
foreach (int value in array)
{
a += value;
}
return a;
}
Anurag SarkarPosted Apr 14, 2014, 12:44 AM EDT
a for loop is a construct that says "perform this operation n. times".
a foreach loop is a construct that says "perform this operation against each value/object in this IEnumerable"
sandip punalkarPosted Apr 21, 2013, 10:15 PM EDT

