Unary operators in C# language is used to increment / decrement the value of a variable or to negate the value of a Boolean variable / Expression etc. Unary operator is performed on only one operand / variable.
Sounds difficult, in fact it’s very easy…Keep reading!!!
We are going to learn about below unary operators in simple way with examples.
++, — and ! (negate) unary operators.
Unary Operators in C# and their operations
++ Increment operator that increment the value by 1
— Decrement operator that decrement the value by 1
! Negate the Boolean value
Note that ! operator also comes under logical operators in C# programming.
Unary operators require only one operand, meaning, unary operators are performed on only one variable.
For example,
If statement is as, int n = 10;
Then, ++ unary operator can be applied on variable n to increment its value by 1 from 10 to 11 as below
n++;
Similarly, — can be applied on variable n to decrement its value by 1 from 10 to 9 as below
n–;
++ ( Increment) Unary operator in C# with Code
++ operator increment the value of variable by 1. For example, in below program, value on n is initialized to 5.
We have applied ++ unary operator on variable n in next statement, that increment the value of n by 1 resulting 6.
On printing the value of variable n, output will be 6.
public class Sample
{
public static void Main(String[] args) {
int n = 5;
n++; //++ increment value of variable n by 1
Console.WriteLine(n);
}
}
Output:
6
Pre Increment & Post increment Unary operators in C#
There are two ways, we can apply ++ operator on variable n,
1)Pre-increment e.g. ++n; //apply ++ before variable n.
2)Post increment e.g. n++ // Apply ++ after the variable n.
PRE-INCREMENT
Pre-increment, first increment the value of a variable, then perform operation.
What does it mean?
Let’s consider the statement b = ++n; in below program.
First increment will be performed. Meaning, first value of n will be incremented then assign the incremented value to variable b.
Hence, if value of n is initially 5, then first it will be incremented to 6 then it will be assigning to b.
So, value of b will be 6.
Note that value of n will also be 6 as it has already been incremented. So, if you print the value on n, it will print 6.
public class Sample
{
public static void main(String[] args) {
//Increment operator that increment the value by 1
int n = 5;
int b;
b = ++n;
Console.WriteLine(b);
}
}
Output:
6
POST-INCREMENT
post increment is just opposite to pre-increment, first value of n will be assigned to variable b, then n will be incremented.
So, value of variable b will be 5.
Then increment happens on n, so if you print the value on n, it’s value will be 6.
public class Sample
{
public static void Main(String[] args) {
int n = 5;
int b;
b = n++;
Console.WriteLine(b);
Console.WriteLine(n);
}
}
Output:
5
6
CONFUSION:
In below program, why the value of n is same i.e.6 when, whatever we write ++n or n++?
public class Sample
{
public static void Main(String[] args) {
int n = 5;
++n; // or n++
//why value prints 6 in case of both
// ++n or n++?
Console.WriteLine(n);
}
}
Output:
6
Answer: Because, on statement ++n or n++, we are not performing any operation, for example, in previous example we did “b = ++n,” or “b =n++;”
We are just incrementing the value by applying any pre-increment [++n] or post increment[n++].
Hence, in next statement, during printing, we are getting incremented value of variable n.
NOTE:
++n or n++ can also be written as n += 1; Notice first +, then = symbol together.
n +=1; is same as n = n+1;
Example:
public class Sample
{
public static void Main(String[] args) {
int n = 5;
n += 1; // same as ++n or n = n +1
Console.WriteLine(n);
}
}
Output:
6
— Unary Operator in C# with Code
— Unary operator decrements the value of a variable by 1.
NOTE: All the rules we studied for ++ unary operator will be applied to – unary operator too. So, just using – instead of ++ in all above examples, you can practice and see the result.
Just an example here.
For example, in below program, value on n is initialized to 5.
We have applied — unary operator on variable n in next statement, that decrement the value of n by 1 resulting 4.
On printing the value of variable n, output will be 4.
public class Sample
{
public static void Main(String[] args) {
int n = 5;
n--; //-- decrement value of variable n by 1
Console.WriteLine(n);
}
}
Output:
4
In brief,
PRE-DECREMENT
–n,
POST-DECREMENT
n–;
n– or –n can be written as n -= 1. It is same as n = n -1.
! (Negate) Unary operator
! negate unary operator just used to reverse/ invert a Boolean value.
It is applied on Boolean variable or Boolean expression.
! Example on Boolean variable.
In the code example, a Boolean variable isDiscount has been created and initialized to false.
In Console.WriteLine isDiscount has been inverted using ! i.e. !isDiscount resulting output as true.
public class Sample
{
public static void Main(String[] args) {
bool isDiscount = false;
//negate the value using ! unary operator
//it will output true
Console.WriteLine(!isDiscount);
}
}
Output:
true
! Example on Boolean Expression.
In this code example, result is of Boolean type and initialized with value false.
C > b; is a Boolean expression, as it compares if c is greater that b resulting true or false.
In this example, value of b is 10 and value of c is 20. So, the expression c > b will result true and true will be assign to variable result.
On negating the variable “result” in Console.WriteLine method as !result, the output will be false.
public class Sample
{
public static void Main(String[] args) {
bool result = false;
int b =10;
int c =20;
result = c > b;// assign true
//negate the value using ! unary operator
//it will output false
Console.WriteLine(!result);
}
}
Output:
False
SPECIAL NOTE:
– is also treaded as Unary operator in some case besides arithmetic operator in C# language.
– unary operator is used to negate the value of a variable.
For example, if variable n has value 10 and you want to assign 10 in another variable b;
Then you can write the statement as b = -n;, Now, b will have value -10.
Note that the value of n will not be changed and it will be 10 only.
public class Sample
{
public static void Main(String[] args) {
bool result = false;
int b =10;
int c =20;
result = c > b;// assign true
//negate the value using ! unary operator
//it will output false
Console.WriteLine(!result);
}
}
RECAP:
Unary operators in C#
++ , — and ! are unary operators in C# programming
++ unary operator increments the value of a variable by 1, it has two form Pre-increment(++n) and post increment (e.g. n++) form
n++ or ++n can be written as n += 1. It is same as n = n +1.
Pre- increment will increment the value of a variable first, then operate it.
In post increment the operation will happen first, then value will be increment.
— decrement the value of a variable by 1.
— unary operator also has two form PRE-DECREMENT i.e.–n, and POST-DECREMENT i.e. n–;
n– or –n can be written as n -= 1. It is same as n = n -1.
! negate unary operator just used to reverse/ invert a Boolean value.