Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Expression or declaration statement expected.
This error occurs when an incorrect control statement is entered when operating in an interactive environment.
The evaluator running in interactive environment (like the Immediate Window of Visual Studio IDE) expects only:
- a single expression (like a value calculation, or method call),
- a variable declaration.
Example
During a debug session, the following statement entered in Immediate Window will return error CS8092:
foreach (int a in [1, 2]) { int b = a++; }
// error CS8092: Expression or declaration statement expected.
To correct this error
Use valid single expressions, without loops blocks or compound statements, which the environment cannot directly interpret.
Below is the same code as in the example, but transformed to a valid statement:
int[] collection = {1, 2};
collection.ToList().ForEach(a => { int b = a++; });
Also, the same code can be turned into a lambda function:
((Action)(() => { foreach (int a in (int[])[1, 2]) { int b = a + 1; }}))()