Introduce ForEach Action (C#)
In This Article
Extracts the content of a foreach loop into a new method, and replaces the loop with an appropriate ForEach method call.
#Purpose
This refactoring allows you to make your code more readable by using newer list iteration syntax.
#Availability
Available from the context menu or via shortcuts:
- when the edit cursor is on a foreach statement iterating through a list or an array. The caret has to be on the foreach keyword.
#Example
private string TestMethod(List<string> strings)
{
string TotalString = "Strings";│foreach (string str in strings)
{
TotalString += String.Format(",{0}", str);
}
}
Result:
private string TestMethod(List<string> strings)
{
string TotalString = "Strings";│strings.ForEach(delegate(string str)
{
TotalString += String.Format(",{0}", str);
});
}