For each x ... Next Vs. For each x ... Next x

I usually program .NET in C #, but I am currently updating a project written in VB.NET and have noticed a curious syntax used in loops For Each.

Is there any difference between

For Each x in collection.Items
    ...
Next

and

For Each x in collection.Items
    ...
Next x

?

I saw both in the code here, and I was curious why someone would use the second option.

+5
source share
4 answers

This is indicated by the MSDN link:

You can optionally specify an element in a Next statement. This improves the readability of your program, especially if you have Nested for each cycle. You must specify the same variable as the one that appears in the corresponding for each operator.

The original can be found here - the fifth section in the "Notes" section:

http://msdn.microsoft.com/en-us/library/5ebk1751.aspx

+5

, , . . . MSDN.

+3

There is no difference in functionality; this is just to avoid errors and backward compatibility with VB6. Personally, I never use it.

+1
source

Here you can annotate which one you nextbelong to for. I am not sure if the compiler verifies that it matches forand next, but maybe it can fail if they don't

0
source

All Articles