C# - TPL2141 logo C# - TPL2141

Types of operator

  1. unary operators
  2. binary operators
  3. ternary operators

(Docs, 2015)

Unary operators

Operators that take 1 operand. For example,

  1. increment operator, ++
  2. new operator, new Friend();
  3. … etc

Binary operators

Operators that take 2 operands. For example,

  1. arithmetic operators
    1. addition, +
    2. subtraction, -
    3. multiplication, *
    4. division, /
    5. modulo, %
  2. logical operators
    1. Logical AND, x & y
    2. Logical XOR, x ^ y
    3. Logical OR, x | y
    4. Conditional AND, x && y
    5. Conditional OR, x || y
  3. .. 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

  1. 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
  2. Docs, M. (2015, July 20). ?: Operator (C# Reference). Retrieved from https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-operator