2.4. Variable Operations

You can use all the standard types of variable operations in C#. When working with numbers, you can use various math symbols, as listed in Table 2-2. C# follows the conventional order of operations, performing exponentiation first, followed by multiplication and division and then addition and subtraction. You can also control order by grouping subexpressions with parentheses:

int number;

number = 4 + 2 * 3;
// number will be 10.

number = (4 + 2) * 3;
// number will be 18.
Table 2.2. Arithmetic Operations
OperatorDescriptionExample
+Addition1 + 1 = 2
-Subtraction (and to indicate negative numbers)5 - 2 = 3
*Multiplication2 * 5 = 10
/Division5.0 / 2 = 2.5
%Gets the remainder left after integer division7 % 3 = 1

Division can ...

Get Beginning ASP.NET 3.5 in C# 2008: From Novice to Professional, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.