Types of operator
Unary operators
Operators that take 1 operand. For example,
- increment operator,
++
- new operator,
new Friend();
- … etc
Binary operators
Operators that take 2 operands. For example,
- arithmetic operators
- addition,
+
- subtraction,
-
- multiplication,
*
- division,
/
- modulo,
%
- addition,
- logical operators
- Logical AND,
x & y
- Logical XOR,
x ^ y
- Logical OR,
x | y
- Conditional AND,
x && y
- Conditional OR,
x || y
- Logical AND,
- .. etc
Ternary operators
Operators that take 3 operands. The only ternary operator in C# is the conditional operator, ?:
(Docs, 2015)
For example, this if-else
statement
if (input > 0)
{
classify = "positive";
}
else
{
classify = "negative";
}
can be written as
classify = (input > 0) ? "positive" : "negative";
References
- Docs, M. (2015, July 20). Operators (C# Programming Guide). Retrieved from https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/operators
- Docs, M. (2015, July 20). ?: Operator (C# Reference). Retrieved from https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-operator