Multiple initialization in C# 'for' loop

Solution 1:

It can't be done. Put one of the declarations before the loop:

MyClass i = 0;
for (int j = 1; j < 3; j++, i++)

Or for symmetry, both of them:

MyClass i = 0;
int j = 1;
for (; j < 3; j++, i++)

It's also possible that one of the variables is more primary than the other. In that case it might be neater to have one be the loop variable, and deal with the other seperately, like this:

MyClass i = 0;
for (int j = 0; j < 3; j++)
{
    ...
    i++;
}

Note that if i and j were of the same type, then you could declare them both in the for-loop:

for (int i = 0, j = 1; j < 3; j++, i++)

Solution 2:

Of course it CAN be done. Just use the dynamic keyword:

public static void Main(string[] args) {
    for (dynamic x = 0, y = new MyClass { a = 20, b = 30 }; x < 100; x++, y.a++, y.b--) {
        Console.Write("X=" + x + " (" + x.GetType() + "\n" +
                      "Y.a=" + y.a + ",Y.b=" + y.b + " (" + y.GetType() + "\n");
     }
}

class MyClass {
    public int a = 0, b = 0;
}

Have a great day!

Solution 3:

Yes, it can be done. You can initialize variables of different types inside a for statement, but you cannot declare variables of different types inside a for statement. In order to initialize variables of different types inside a for statement you must declare all the types before the for loop. For example:

int xx;
string yy;
for(xx=0, yy=""; xx<10; xx++)
    {
    ....
    }

[EDIT] Adding more information for completeness. This goes beyond what the OP requested, but may be helpful for others. It is simple to initialize variables of the same type in a for loop, just separate the initialization by commas. You can also have multiple variables changed in the third section. You cannot have multiple comma separated sections in the second, comparison section, but you can use && || and ! to make a complex Boolean section based on multiple variables.

for(int i=0, j=0, k=99; i<10 && k<200; i++, j++, k += 2)

However, it is not a good practice to make a for statement that is so complex that it is difficult to understand what is going on.

Solution 4:

Is it harmful?

Yes, very much so. A language parser has two important duties. One is the job that everybody is familiar with, convert text into an executable program. But very important as well is that it can detect an invalid program and generate a meaningful diagnostic to the programmer so he can fix his code.

At a very fundamental level, a language parser distinguishes between declarations, statements and expressions. The curly-brace languages do obfuscate that distinction, you can turn any expression into a statement, simply by putting a semi-colon after it. And in some cases accept a declaration inside a statement, the for(;;) statement is a good example. Most plainly, this syntax is perfectly acceptable in the C or C++ languages:

int x = 42;
x;

That's not exactly a good thing, it is nonsense code. The C# language upped the bar on that, it will reject that. But not:

int x = 42;
x++;

A special rule added to the language parser to accept this.

What none of the curly-brace languages will accept is turning a declaration into an expression. That way lies madness, dragons on the end of the map, ship falls off the edge with no good message left to report. The comma operator requires the lefthand and righthand operands to be expressions. A declaration is not an expression, end of story.