Refactoring is the process of modifying code in order to make it easier to maintain, understand, and extend, but without changing its behavior.
In some cases, you might want to convert a for
loop to a foreach
statement to avoid the local loop variable inside the loop or to simplify your code and reduce the likelihood of logic errors in the initializer, condition, and iterator sections.
Let's consider the following simple example of for
loop which we can easily convert to foreach
statement using code refactoring.
using System;
using System.Collections.Generic;
using System.IO;
namespace VisualStudioTutorial
{
class Program
{
static void Main(string[] args)
{
List<Customer> customers = new List<Customer>();
customers.Add(new Customer { Id = 1, Name = "John" });
customers.Add(new Customer { Id = 2, Name = "Mark" });
customers.Add(new Customer { Id = 3, Name = "Stella" });
for (int i = 0; i < customers.Count; i++)
{
int id = customers[i].Id;
string name = customers[i].Name;
Console.WriteLine("{0}, {1}", id, name);
}
}
class Customer
{
public int Id { get; set; }
public string Name { get; set; }
}
}
}
Now you can convert the for
loop by placing your mouse cursor in the for
keyword and click the screwdriver icon in the margin of the code file.
You can select the Convert to 'foreach' option if you want to change the code, but if you want to see the changes before, select Preview changes to open the Preview Changes dialog. So let's click on the Preview changes.
Click the Apply button and you will see that for
loop is converted to foreach
statement.
foreach (Customer v in customers)
{
int id = v.Id;
string name = v.Name;
Console.WriteLine("{0}, {1}", id, name);
}
In some cases, you might want to convert a foreach
statement to a for
loop to use the local loop variable inside the loop for more than just accessing the item or you are iterating through a multi-dimensional array and you want more control over the array elements.
Let's consider the following example.
foreach (Customer customer in customers)
{
int id = customer.Id;
string name = customer.Name;
Console.WriteLine("{0}, {1}", id, name);
}
Similarly, you can convert the foreach
statement by placing your mouse cursor in the for
keyword and click the screwdriver icon.
Click the Convert to 'for' option and you will see that the forearch
statement is converted to the following for
loop.
for (int i = 0; i < customers.Count; i++)
{
Customer customer = customers[i];
int id = customer.Id;
string name = customer.Name;
Console.WriteLine("{0}, {1}", id, name);
}