An operator is a symbol (such as =, +
, or >
) that causes C# to take an action. That action might be an assignment of a value to a variable, the addition of two values, a comparison of two values, and so forth. In that respect, most C# operators aren't much different from the ones you remember from math class, and they're intended to be just that intuitive. There are some special operators whose meanings aren't obvious, and we'll cover those, too.
In the preceding chapter, you were introduced to the assignment operator. The single equals sign (=
) is used to assign a value to a variable; in this case, the value 15 to the variable myVariable
:
myVariable = 15;
C# has many different operators that you'll learn about in this chapter. There's a full set of mathematical operators, and a related set of operators just for incrementing and decrementing in integral values by one, which actually are quite useful for controlling loops, as you'll learn in Chapter 5. Operators are also available for comparing two values that are used in the branching statements, as we'll also demonstrate in Chapter 5.
Any statement that returns a value is an expression. You've already seen the assignment expression, which we'll discuss in more detail in a moment, and we've mentioned that the assignment expression returns the value that's assigned. In this chapter, you'll see a number of mathematical expressions, which return a computed value, and also comparison expressions, which return the Boolean value true
or false
.