For Vs Foreach In C#

There are many programmers who are still confused when to use ‘for’ loop and when to use ‘foreach’ loop. In this article, I will try to reduce that confusion.

While programming, you should not only focus on the output but the performance too. In this article, I will be covering both, ‘for’ loop and ‘foreach’ loop (basic working and performance also). I will be focusing on the loops on lists or collections in this article.

Firstly let’s see how both loops work.

For loop

The ‘For’ loop will iterate until a certain condition is true. You give an initial statement, a condition for which the loop is to be iterated until it gets false, and a statement which will be executed after every successful block execution.

A normal For loop looks like this.

  1. int length = 100;  
  2.            for (int index = 1; index < length; index++)  
  3.            {  
  4.                //Your code will be here  
  5.                //This is how a programmer makes century.....#ilovecricket  
  6.            }  

A For loop on the list will be like this.

  1. List<JCEmployee> JCEmployees = GetJCEmployeesList();  
  2.            for (int index = 0; index < JCEmployees.Count; index++)  
  3.            {  
  4.                Console.WriteLine(JCEmployees[index].Age);  
  5.            }  

Note here that you can directly access the list item by its index.

Foreach loop

The ‘Foreach’ loop is used to iterate over items in collections (Lists, Arrays etc.).

When you have a list of items, then instead of using a For loop and iterate over the list using its index, you can directly access each element in the list using the ‘Foreach’ loop.

A normal ‘Foreach’ loop looks like this.

  1. List<JCEmployee> JCEmployees = GetJCEmployeesList();  
  2. foreach(JCEmployee Employee in JCEmployees)  
  3.             {  
  4.                 Console.WriteLine(item.Age);              
  5.             }  

Note here that a ‘foreach’ loop creates a copy of the collection on which you are iterating the loop. This means, if you want to perform an assignment operation on the collection item, you cannot directly perform on that item.

To understand that, look at the following example.

For Vs Foreach In C#
 
Here is a detailed article and code samples on the foreach loop - The foreach in C#.

Why did that error occur here?

As I said, ‘Foreach’ loop creates a copy of the collection, so in this loop, the ‘item’ is not referencing to the array elements; it’s just a temporary variable and you cannot assign a value to it.

Also, when it comes to performance, then ‘foreach’ takes much time as compared to the ‘for’ loop because internally, it uses extra memory space, as well as, it uses GetEnumarator() and Next() methods of IEnumerables.

So, the long story short is that when you want your code to be readable and clean, you can use a Foreach loop but whenever you want the performance, you should use a For loop instead.

Thank you guys. I hope you like this article. If you have any doubts, please comment below.