Unary and Binary Operators
Explore unary and binary operators in C# to understand how they manipulate single and multiple operands. Learn the difference between prefix and postfix unary operators and how to use common arithmetic operators for calculations. This lesson helps you write clear and predictable code using these fundamental operators.
Operators apply simple operations such as addition and multiplication to operands such as variables and literal values. They usually return a new value that is the result of the operation, and that can be assigned to a variable.
Binary operator
Most operators are binary, meaning that they work on two operands, as shown in the following pseudo-code:
var resultOfOperation = firstOperand operator secondOperand;
Example
Examples of binary operators include adding and multiplying, as shown in the following code:
Unary operator
Some operators are unary, meaning they work on a single operand, and can apply before or after the operand, as shown in the following pseudocode:
Example
Examples of unary operators include incrementors and retrieving a type or its size in bytes, as shown in the following code:
Ternary Operator
A ternary operator works on three operands, as shown in the following pseudocode: ...