Convert "foreach" to "for" loop or vice versa with ReSharper?

Is it possible to mark a foreach loop code block and convert it to a for loop using ReSharper?

Or with Visual Studio?

Thank!

+5
source share
2 answers

Yep ReShaper can do this. Tested in VS2010 + R # 5

Before:

        var a = new int[] {1, 2, 3, 4};
        foreach (var i in a)
        {

        }

After:

    var a = new int[] {1, 2, 3, 4};
    for (int index = 0; index < a.Length; index++)
    {
        var i = a[index];
    }
+3
source

works fine, as rdkleine said, and the sample works fine.
BUT: if your collection is simple IEnumerable<T>, it will not work (reasonably).

0
source

All Articles