Operating on variables
Operators apply simple operations, such as addition and multiplication, to operands, such as numbers. They usually return a new value that is the result of the operation.
Most operators are binary, meaning that they work on two operands:
var result = FirstOperand operator SecondOperand;
Some operators are unary meaning they work on a single operand. A ternary operator works on three operands.
Experimenting with unary operators
Two common unary operators are used to increment ++
and decrement --
a number.
In Visual Studio, from the View menu, choose Other Windows, and then C# Interactive. Enter the following code:
> int i = 3; > i 3
Note that when you enter a full statement ending in a semicolon, it is executed when you press Enter.
The first statement uses the assignment operator =
to assign the value 3
to the variable i
. When you enter a variable name at the prompt, it returns the variable's current value.
Enter the following statements and before pressing Enter...