Understanding the difference between expressions and statements
At its core, an expression in C# is just a piece of code that evaluates to a value. Simple expressions include constants, variables, and method calls. On the other hand, a statement is a standalone unit of code that performs an action. In essence, it is an executable instruction. The best way to understand something is through practice. So, let’s not delay anymore and look at expressions and statements through examples.
Example of expressions
Consider the following C# code:
var pagesPerChapter = 20; var totalBookPages = pagesPerChapter * 10;
In this snippet, 20, pagesPerChapter
, 10
, and pagesPerChapter * 10
are all expressions. Each of these pieces of code evaluates to a value.
Example of statements
Now, let’s identify statements:
var pagesPerChapter = 20; var totalBookPages = pagesPerChapter * 10;
Here, var pagesPerChapter = 20; and var totalBookPages = pagesPerChapter * 10; are statements...