Skip to content

VB -> C#: Avoid loopTo in for loops if possible #1243

@circular17

Description

@circular17

Thanks for the wonderful tool. Here is an improvement that would make code conversion much easier.

When converting For loops from VB.NET to C#, if the final value of the range is not constant, a loopTo variable is added. While it makes sense from a semantic point of view, this makes the code slightly less readable.

If there are multiple for loops, we end up with a series of loopTo, loopTo2, loopTo3 etc. It would be wonderful if this could be simplified if possible.

For example, the following VB.NET code:

For i As Integer = 1 To n
Next

Is translated as:

for (int i = 1, loopTo = n; i <= loopTo; i++) { }

Proposed solution
When the upper bound is a local variable that is not explicited modified or passed as reference in the loop, simply use that variable instead of creating a duplicate. In the previous example, this would be:

for (int i = 1; i <= n; i++) { }

Another handy improvement would be when iterating from 0 to n - 1 when the loop variable is an integer.

For i As Integer = 0 To n - 1
Next

Would be translated using a strict comparison:

for (int i = 0; i < n; i++) { }

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions