Unary operators

An operator that requires only one operand is called a Unary operator. They can perform operations such as increment, decrement, negation and so on. They can also be applied before (prefix) or after (postfix) the operand.

The following table lists a few Unary operators. x, in the left-hand column, is an operand where we apply the operators:

Expression Description
+x Identity: This operator can be used as a Unary or Binary operator. If it's used on numeric values, it returns the value. If it's applied on two numeric operands, it returns the sum of operands. On strings, it concatenates both operands.
-x Negation: This operator can be used as a Unary or Binary operator. Applying this operator on numeric types results in the numeric negation of the operand.
!x Logical negation: This operator negates the operand. It is applied for bool operands and returns if the operand is false.
~x Bitwise negation: This produces a complement of its operand by reversing each bit.
++x Pre-increment: This is an increment operator and can appear before or after the operand. When prefixed, the result is placed after the increment. If postfixed, the result is placed before the increment.
--x Pre-decrement: This is a decrement operator and can appear before or after the operand. When prefixed, the result is placed after decrement. If postfixed, the result is placed before the decrement.

In the following code, we will declare a few variables and use them to showcase examples of the preceding operators:

int firstvalue = 5; 
int secondvalue = 6;
string firststring = "Hello ";
string secondstring = "World";

+ and - can be used with a single operand or multiple operands. When used with multiple operands of the integer type, they either sum the operands or get the difference. The + operator can be used with string type operands as well. In this case, it will concatenate both the strings. A string and an operator is always a Binary operator:

//'+' operator
Console.WriteLine(+firstvalue); // output: 5
Console.WriteLine(firstvalue + secondvalue); // output: 11
Console.WriteLine(firststring + secondstring); // output: Hello World
//'-' operator
Console.WriteLine(-firstvalue); // output: -5
Console.WriteLine(firstvalue - secondvalue); // output = -1

The ! operator works well with Boolean operands where it produces logical negation; that is, true becomes false, whereas the ~ operator works with bitwise operands. In the following example, a Binary digit representation and its bitwise negation are displayed. We are taking an integer value and converting it into a Binary value and then negating it using the ~ operator and displaying it in base 2 format:

//'!' operator
Console.WriteLine(!true);

//output : false

//'~' operator
Console.WriteLine("'~' operator");
int digit = 60;
Console.WriteLine("Number is : {0} and binary form is {1}:", digit, IntToBinaryString(digit));
int digit1 = ~digit;
Console.WriteLine("Number is : {0} and binary form is {1}:", digit1, Convert.ToString(digit1, 2));

//Output:
Number is : 60 and binary form is 111100:
Number is : -61 and binary form is 11111111111111111111111111000011

The ++ and -- operators, when applied on integer operands, perform increments or decrements on the operands, respectively. These can be applied pre or post an operand. The following example shows both the post and pre increment and decrement operators. Pre produces results before displaying and post produces results after displaying:

// '++' Operator
Console.WriteLine(++firstvalue); // output: 6
// '--' Operator
Console.WriteLine(--firstvalue); // output: 5
// '++' Operator
Console.WriteLine(firstvalue++); // output: 5
Console.WriteLine(firstvalue--); // output: 6
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.178.211