What is the difference between "for" and "foreach" loop in C#?

Which one is better in performance wise and why we preferr for loop?

Giribabu
Views: 25535 | Community Opinion: 5

Tags..  C# Interview  C# Tutorial

Bookmark this page..



Ask a New Question Go to Home

Community Opinion/Answers
 

C# for loop

The for loop executes a statement or a block of statements repeatedly until a specified expression evaluates to false. The for loop is useful for iterating over arrays and for sequential processing.

Because the test of a conditional expression occurs before the execution of the loop, a for statement executes zero or more times.

C# for loop Example

class ForLoopTest
{
static void Main()
{
for (int i = 1; i <= 5; i++)
{
Console.WriteLine(i);
}
}
}




foreach loop in C#

The foreach statement repeats a group of embedded statements for each element in an array or an object collection. The foreach statement is used to iterate through the collection to get the desired information, but should not be used to change the contents of the collection to avoid unpredictable side effects.

C# foreach loop Example

class ForEachTest
{
static void Main(string[] args)
{
int[] fibarray = new int[] { 0, 1, 2, 3, 5, 8, 13 };
foreach (int i in fibarray)
{
System.Console.WriteLine(i);
}
}

}


foreach loop may (not always, but depends) reduce the performance of the code. Therefore for loop is recommended.




Vibram Five Fingers Said..

Before deciding to buy Vibram Five Fingers, you should consult Vibram Five Fingers uk a specialist who will consider walking five fingers shoes, your stride, the type of Vibram fivefingers, weight and other Vibram shoes factors put you in the right shoe. The basic Vibram Five Fingers sale properties of walking Vibram Bikila FiveFingers should be flexible, flat Vibram Classic FiveFingers and the heel is not flared. When the Vibram Flow FiveFingers are flexible you will be able to bend and twist the Vibram Kso FiveFingers. When you go forward with your Vibram Kso Trek FiveFingers, your step will flex Vibram Five Fingers Speed Shoes White-Black as you roll through Vibram 5 Fingers Treksport Black a step from heel to toe. In other words, if the Vibram FiveFingers Bikila Women Sky_Blue-Grey is too stiff, your foot will fight Vibram Five Fingers Kso Women Black with every step you take. Make sure you invest in the right Vibram Five Fingers Sprint Mens Shoes Yellow-Black to avoid injury and keep you walking Vibram Speed FiveFingers in comfort and convenience.




maneesh Said..

foreach loop may (not always, but depends) reduce the performance of the code. Therefore for loop is recommended.
The foreach statement repeats a group of embedded statements for each element in an array or an object collection. The foreach statement is used to iterate through the collection to get the desired information, but should not be used to change the contents of the collection to avoid unpredictable side effects




Nagi Setty A Said..

List MyList = new List();
MyClass MyObject = new MyClass();
MyObject.i = 0;
MyObject.name = "name";
MyList.Add(MyObject);

for (int i = 0; i <= MyList.Count; i++)
{
MyList.Add(MyObject); //No Error
Console.WriteLine(i);
}
foreach (MyClass MC in MyList)
{
MyList.Add(MyObject); //It will reports error. that collection modified
Console.WriteLine(MC.i);
}
Console.ReadKey();




ashish Said..

Related Questions & Answers:

Can you store multiple data types in System.Array?

Interface and abstract classes

Difference between C# and C programming Language?

What is difference between "is" and "as" keyword in C#?

How do we define INPUT and OUTPUT parameters in SQLCLR?

What is the difference between Hash Table and Arrays?






What do you think? Add your opinion/answer
*Name
*your opinion/answer: