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 ...

Get C# 6 and .NET Core 1.0: Modern Cross-Platform Development 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.