Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Open

Description

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
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

        AltStyle によって変換されたページ (->オリジナル) /