Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The C# documentation Introduction - C# language specification | Microsoft Docs[^] gives the following code example demonstrating what is not an expression statement and what is an expression statement:

C#
static void Main() 
    {
        int i;
        i = 123;       // Expression statement
    }



But what if I combine both lines of code into one line of code. Is the bottom line of code an expression statement or is it not an expression statement:

C#
int i = 123;


What I have tried:

No online resource specifies the answer.
Posted
Updated 30-Nov-21 17:11pm
v2
Comments
Richard MacCutchan 1-Dec-21 4:02am    
The first example is a throwback to the early days of the C programming language, when variables had to be declared at the beginning of a block. With more modern languages and variants (C++, C#, Java etc.) it is generally accepted that declaration and initialisation (the second example) should happen together. In fact most compilers will raise a warning for uninitialised declarations.

1 solution

To directly answer your question: it is not an expression statement; it is a declaration statement with initialization.

The difference between an expression and a statement is that the execution of a statement doesn't produce a value whereas an expression does. So statements have side-effects but produce no value while expressions can both have side-effects and produce a value. Multiple expressions can make up a statement but there can only be one statement per "line" (line defined by ;).

C#
int i;           //No resulting value but creates an integer i as a side-effect.
i = 123;              //Resulting value is 123; side-effect assigns 123 to i.
Console.WriteLine(i); //Resulting value is void; side-effect prints 123 to console.
++i;                  //Resulting value is 124; side-effect increments i to 124.
i++;                  //Resulting value is 124; side-effect increments i to 125.
Console.WriteLine(i); //Resulting value is void; side-effect prints 125 to console.

"Expression statements" are just expressions that are used for their side-effects only and the result of the expression is ignored; it's an expression used as if it were a statement. So above with i++, we're indicating we only care about the side-effect of incrementing i and not the return value. Another example:
C#
j = i = 123; //Result is 123; side-effects assign 123 to both i and j.

Here the whole j = i = 123 is an expression statement that uses the expression i = 123. But with
C#
int j = i = 123; //No result; side-effects create integer j, assign 123 to i and j.

int j = i = 123 is a declaration statement that uses the expression i = 123.

Hope that helps.
 
Share this answer
 
v3
Comments
Richard MacCutchan 1-Dec-21 3:58am    
According to the MSDN documentation it is an expression statement. Which surprised me too.
Jon McKee 1-Dec-21 4:32am    
If you're talking about the linked docs, that first line "int i;" under "Expression Statements" is the only one without an "expression statement" comment ;)
Richard MacCutchan 1-Dec-21 4:36am    
It's little wonder that newbies get confused.
OldBounty 1-Dec-21 14:40pm    
I too initially believed I that the declaration and initialization of a variable is called expression statement. But the documentation is not clear on whether it is or is not.

Jon Mckee means well but states otherwise and I am led to believe his answer is correct. But why do you say that ... int i = 123; ... is an expression statement?
Richard MacCutchan 2-Dec-21 4:19am    
The documentation gives the correct meaning. But don't get hung up on semantics, it really does not matter what you call it, as long as you understand what it does.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900