Expressions and Operators

An expression essentially denotes a value. The simplest kinds of expressions are constants and variables. Expressions can be transformed and combined using operators. An operator takes one or more input operands to output a new expression.

Here is an example of a constant expression:

	12

We can use the * operator to combine two operands (the literal expressions 12 and 30), as follows:

	12 * 30

Complex expressions can be built because an operand may itself be an expression, such as the operand (12*30) in the following example:

	1 + (12 * 30)

Operators in C# are classed as unary, binary, or ternary— depending on the number of operands they work on (one, two, or three). The binary operators always use infix notation, where the operator is placed between the two operands.

Primary Expressions

Primary expressions include expressions composed of operators that are intrinsic to the basic plumbing of the language. Here is an example:

	Math.Log (1)

This expression is composed of two primary expressions. The first expression performs a member-lookup (with the . operator), and the second expression performs a method call (with the () operator).

Void Expressions

A void expression is an expression that has no value. For example:

	Console.WriteLine (1)

A void expression—because it has no value—cannot be used as an operand to build more complex expressions:

	1 + Console.WriteLine(1)       // Compile-time error

Assignment Expressions

An assignment expression uses the = operator to assign ...

Get C# 3.0 Pocket Reference, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.