Expressions vs. Statements in C#

When I first learned how to program I was using a programming language (Java) without really understanding the basic grammar. I was taught the principles of Object Oriented programming, the difference between a class and an object, value types, reference types and boxing, namespaces and packages and other high-level aspects of programming. Of course the low-level concerns like control flow, variables, assignment and method calls were also taught, but I don’t remember ever getting down to the absolute basics of the language: statements and expressions. And I have never missed it, I could write software without knowing about it. But there is value in learning the distinction, it will help you write more concise code.

The difference between expressions and statements

Expressions and statements are the building blocks of your software. But what is the difference between these two constructs? Simply put, an expression is a construct that evaluates to a value, a statement is a unit of execution. Let’s illustrate:

Console.WriteLine("Hello World!");
int answer = 42;

The whole first line is a statement and so is the second line, they are both a unit of execution. The highlighted parts are expressions, the first expression evaluates to the value "Hello World!" of type String, the second expression evaluates to the value 42 of type Int32. Expressions that evaluate to the same type of value can be combined to create compound expressions, so this is a valid expression: 1 + 2 - 3. If you put that on a line and try to compile your program, the compiler will complain about a missing semicolon (compiler error CS1002):

; expected

If you add the semicolon at the end of the expression and compile, the compiler will still complain (compiler error CS0201):

Only assignment, call, increment, decrement, and new object expressions can be used as a statement

So expressions can be used as statements simply by putting a semicolon at the end of it, but this is only valid for certain types of expressions. According to MSDN 1 + 2 - 3; is a meaningless statement! The meaningful statements that the compiler does accept are called expression statements (yes, confusing). Then there are declaration statements and empty statements:

// Declaration statement
int answer;

// Expression statements
answer = 42;
DoSomething();
answer++;
answer--;
Object thing = new Object();

// Empty statement
;

To complete our understanding, block statements are collections of statements inside balanced braces. They can be used anywhere a single statement can be used.

So what?

So far, it is all theory, and no practice. Like I said, it’s possible to program in C# without knowing exactly the difference between expressions and statements. But have you ever wondered about the difference between the conditional operator ?: and the if-else construction? The conditional operator is an expression (it evaluates to a value) whereas the if-else construction is a statement (a control flow statement to be precise, the last type of statement). This means that we can use the conditional operator anywhere we can use expressions, for instance as the parameter to a method:

Console.WriteLine(name != null ? name : "Missing name");

You obviously can’t do that with an if-else statement. You knew this, but you may not have known exactly why. Now that you do, you can start using expressions more consciously, like in the following example where we initialize a collection to prevent a null reference exception when accessing the property:

private List<File> files;
public List<File> Files
{
    get { return files ?? (files = new List<File>(); }
    set { files = value; }
}

We use the null-coalescing operator ?? to test the value of the private field for null. This operator will return the left-hand operand if it is not null, and the right-hand operand otherwise. For the right-hand operand we use an expression to initialize the private field. Since it is an expression it has a value. This value becomes the right-hand operator and is thus the result of the ?? operator and is returned (and thus becomes the parameter value for the method). We can use the ?? operator in the previous example as well:

Console.WriteLine(name ?? "Missing name");

Conclusion

In this post I explained the difference between expressions and statements. Expressions have a value, statements are a unit of execution. We have seen that when you understand the value of expressions, you can start using them more consciously to write more elegant and concise code.