Invalid token 'while' in class, struct, or interface member declaration in very simple code

Based on the error, it sounds like the compiler thinks this code is typed directly in the body of a class/struct/interface declaration. Statements while/if/for/etc ... must appear with in a method.

Try moving this code into a method to fix the problem. If it's in a method, you likely have a mismatched brace problem.


There's nothing wrong with the while, it's something above it that's the problem. Check for mismatched braces and semicolons in a comment or something like that.


C# is not allowed to write code directly into the classes; it is allowed to write only data members and function members directly into the classes.