Skip to main content

ForEach to For

Converts a foreach (ForEach) loop to a for (For) loop.

#Availability

Available from the context menu or via shortcuts:

  • when the caret is on a foreach (For Each) keyword.

#Example

private string TestMethod(List<string> myStrings)
{
    string result = String.Empty;foreach (string str in myStrings)
        result += String.Format(".{0}", str);
    return result;
}
Private Function TestMethod(ByVal myStrings As List(Of String)) As String
    Dim result As String = String.EmptyFor Each Str As String In myStrings
        result += String.Format(".{0}", Str)
    Next
    Return result
End Function

Result:

private string TestMethod(List<string> myStrings)
{
    string result = String.Empty;for (int i = 0; i < myStrings.Count; i++)
        result += String.Format(".{0}", myStrings[i]);
    return result;
}
Private Function TestMethod(ByVal myStrings As List(Of String)) As String
    Dim result As String = String.EmptyFor i As Integer = 0 To myStrings.Count - 1 Step 1
        result += String.Format(".{0}", myStrings(i))
    Next
    Return result
End Function

#Screenshot

rsForeachToFor

See Also