Can every statement be an expression in C#? For example I know that a method invocation statement can be used as an expression and because of that I can do stuff like this:
for (int i = 0; i < 10; Console.WriteLine(i++));
But is it the case with every statement?
Edit: But this thing for some reason doesn't work with the while loop
while (Console.WriteLine(1) > 0) { }
This code gives an error. Maybe you can explain what's happening? I got a bit confused.
In both cases you need an empty statement:
The empty statement consists of a single semicolon. It does nothing and can be used in places where a statement is required but no action needs to be performed.
The reason why for(int i = 0; i < 10; Console.WriteLine(i++));
works is because it has an empty statement ;
at the end of the block.
To make the while loop work you need to add an empty statement as well.
while(Console.WriteLine(1) > 0);