You might
have used binary operators frequently in your code. Binary operators operate
on two operands and give the result. But C# also provides a set of operators
which work only on a single operand. Such operators are termed as Unary
operators. This article will focus on different unary operators provided
by C#.
C# provides six unary operators. They are listed below:
Each of these
operators is discussed with relevant examples in rest of the article.
Unary +:
Unary + operator are used only in logical sense. They do not perform any
operation on the operand. You generally represent a negative number using
– sign. Similarly unary + is used to represent a positive number.
Even if you use this operator along with an operand having negative value,
it doesn’t make any difference in the operand’s value. Since
you have a unary – operator, you have unary + operator as its complement.
Here is an example using unary + operator:
class sampleClass
{
public static void Main() {
int positiveInteger = +10;
int negativeInteger = -10;
negativeInteger = +negativeInteger;
Console.WriteLine(“ The negativeInteger value is :” + negativeInteger);
}
}
Output of
this code will be:
The negativeInteger
value is : -10
Note that
the value of negativeInteger is still -10. Unary + operator didn’t
have any impact on it.
Unary –:
Unary – operator is used to represent a negative value as shown in
the example below. When you apply – operator on an operand with negative
value, the value becomes positive. This is demonstrated in the example
below:
class sampleClass
{
public static void Main() {
int positiveInteger = 10;
int negativeInteger = -10;
positiveInteger = -positiveInteger;
negativeInteger = - negativeInteger;
Console.WriteLine(“positiveInteger = {0}, negativeInteger = {1}”,
positiveInteger ,
negativeInteger);
}
}
Output of
this code will be:
positiveInteger
= -10 negativeInteger = 10
Note that
the positiveInteger has negative value and negativeInteger has positive
value when operated upon by unary – operator.
Increment
Operator ++: Increment operator is used to increment the operand’s
value by 1. The increment statement can be assigned to another variable.
There are two forms of usage of increment operator:
• Pre-Increment:
The operand will be incremented and the incremented value is assigned
to the variable. Hence the operand and the assigned variable have the
same value.
• Post-Increment: The operand value will be assigned to the variable
and the operand is then incremented. In this case, operand value will
be greater than the assigned variable value.
•
Both pre-increment and post-increment of increment operator ++ is demonstrated
below:
class sampleClass
{
int sampleVar1 = 10;
int sampleVar2 = 10;
int sampleVar3 = ++sampleVar1; //Pre-Increment
int sampleVar4 = sampleVar2++; //Post-Increment
Console.WriteLine(“Pre-Increment: sampleVar1 = {0}, sampleVar3 =
{1}”, sampleVar1,
sampleVar3);
Console.WriteLine(“Post-Increment: sampleVar2 = {0}, sampleVar4 =
{1}”, sampleVar2,
sampleVar4);
}
Decrement
Operator --: Decrement operator is used to decrement the operand’s
value by 1. Similar to increment operator, decrement operator can also
be used in two different ways:
• Pre-Decrement: The operand will be decremented and then assigned
• Post-Decrement: The operand will be assigned first and then decremented
This is demonstrated
in the example below:
class sampleClass
{
int sampleVar1 = 10;
int sampleVar2 = 10;
int sampleVar3 = - -sampleVar1; //Pre-Decrement
int sampleVar4 = sampleVar2- -; //Post-Decrement
Console.WriteLine(“Pre-Decrement: sampleVar1 = {0}, sampleVar3 =
{1}”, sampleVar1,
sampleVar3);
Console.WriteLine(“Post-Decrement: sampleVar2 = {0}, sampleVar4 =
{1}”, sampleVar2,
sampleVar4);
}
Logical Negation
Operator !: This operator is used to negate the Boolean value. If the
Boolean value is true then using ! on that Boolean value will result in
false. Normally this operator is used in conditional checks. Here is an
example to demonstrate this operator:
class sampleClass {
public static void Main() {
bool value1 = true;
bool value2 = !value1;
Console.WriteLine(“value1 = “ + value1 + “ value2 = “
+ value2);
}
}
Output of
this code will be:
value1 =
true value2 = false
Bitwise Complement
Operator ~: Any number you specify will be internally converted into binary
number containing bits. If you want to complement or invert each of the
bit in a binary digit, then you can do it using bitwise complement operator
~. Consider the following example:
class sampleClass {
public static void Main() {
byte value1 = 15;
byte value2 = ~value1;
Console.WriteLine(“value1 = “ + value1 + “ value2 = “
+ value2);
}
}
Output of
this code will be:
value1 =
15 value2=240
How did you
arrive at this output? Binary equivalent of 15 is 00001111. When you use
~, it becomes 11110000 which is equivalent to 240.
FREE
Subscription
Subscribe
to our mailing list and receive new articles
through email. Keep yourself updated with latest
developments in the industry.
Note
: We never rent, trade, or sell my email lists to
anyone.
We assure that your privacy is respected
and protected.