Don't miss an insight. Subscribe to Techopedia for free.

Subscribe
Advertisements

Unary Operator

What Does Unary Operator Mean?

A unary operator, in C#, is an operator that takes a single operand in an expression or a statement. The unary operators in C# are +, -,!, ~, ++, — and the cast operator.

Advertisements

The signature of the declaration of a unary operator includes the operator token and the type of parameter; it does not require the return type and the name of the parameter.

All the C# unary operators have predefined implementation that will be used by default in an expression. These unary operators can be overloaded in user-defined types with custom implementation by defining static member functions using the "operator" keyword.

Techopedia Explains Unary Operator

The list of unary operators with their details includes:

  • Unary Plus Operator (+): The result of an operation on a numeric type is the value of the operand itself. This operator has been predefined for all numeric types.
  • Unary Minus Operator (-): This operator can be used to negate numbers of the integer, floating-point and decimal type.
  • Logical Complement (negation) Operator (!): This operator can be used only with operands of Boole type.
  • Bitwise Complement (negation) Operator (~): This operator can be used with integer, unit, long and ulong operand types. The result of the operation is a bitwise complement (inverse of the binary representation) of the operand.
  • Prefix Increment (++) and Decrement (–) Operator: The operand can be a variable, property access, or an indexer access. With an increment operator, the result of the operation for operands of integer type would be the value incremented by 1. With a decrement operator, the result would be the value decremented by 1 from the operand. The increment/decrement operator can also be used with postfix notation
  • Cast Operator: Used to build cast expressions for conversion to a given type. This operator is represented by the symbol, "T," where T is the type to which the operand or the result of the expression must be converted

Advertisements

Related Terms