An expression is a combination of values, variables, operators, and calls to methods. An expression can be thought of as a formula that is made up of multiple pieces.
The evaluation of an expression produces a value, known as the return value. We say that an expression returns a value.
Expressions need to be evaluated when the code executes in order to determine the return value, or specific piece of data that should be used. Evaluation is the process of computing the return value.
If you ask C# to print an expression using
Console.WriteLine, the interpreter evaluates the expression and displays the result.
Console.WriteLine(1 + 1);
This code does not print
1 + 1 but rather the result of calculating
1 + 1. In other words,
Console.WriteLine(1 + 1) prints the value
2. This is what we would expect.
Since evaluating an expression produces a value, expressions can appear on the right-hand side of assignment statements.
int sum = 1 + 2; Console.WriteLine(sum);
The value of the variable
sum is the result of evaluating the expression
1 + 2, so the value
3 is printed.
A value all by itself is a simple expression, and so is a variable. Evaluating a variable gives the value that the variable refers to. This means that line 2 of the example above also contains the simple expression