The unary operators enable operations with simpler syntax than a comparable binary operation. The unary operators include + (plus), - (minus), ++ (increment), -- (decrement), ! (logical negation), and ~ (bitwise complement).
Increment Operator:(++)
The increment operator (++) allows incrementing the value of a variable by 1. The timing of the effect of this operator depends upon which side of the expression it's on.
Here's a post-increment example:
int count;
int index = 6;
count = index++; // count = 6, index = 7
In this example, the ++ operator comes after the expression index. That's why it's called a post-increment operator. The assignment takes place, and then index is incremented. Because the assignment occurs first, the value of index is placed into count, making it equal to 6. Then index is incremented to become 7.
Here's an example of a preincrement operator:
int count;
int index = 6;
count = ++index; // count = 7, index = 7
This time, the ++ operator comes before the expression index. This is why it's called the preincrement operator. index is incremented before the assignment occurs. Because index is incremented first, its value becomes 7. Next, the assignment occurs to make the value of count equal to 7.
Example:
class Program { static void Main(string[] args) { int Value = 2; int preIncrement; int postIncrement; preIncrement = ++Value; Console.WriteLine("PreIncrement Value is: {0}", preIncrement); Console.ReadLine(); Console.WriteLine("Value is:{0}", Value); Console.ReadLine(); postIncrement = Value++; Console.WriteLine("PostIncrement Value is: {0}", postIncrement); Console.ReadLine(); Console.WriteLine("Value is:{0}", Value); Console.ReadLine(); } } |
Output:
PreIncrement Value is: 3
Value is:3
PostIncrement Value is:3
Value is:4
Decrement Operator:(--)
The decrement operator (--) allows decrementing the value of a variable. The timing of the effect of this operator again depends upon which side of the expression it is on. Here's a post-decrement example:
int count;
int index = 6;
count = index--; // count = 6, index = 5
In this example, the -- operator comes after the expression index, and that's why it's called a post-decrement operator. The assignment takes place, and then index is decremented. Because the assignment occurs first, the value of index is placed into count, making it equal to 6. Then index is decremented to become 5.
Here's an example of a predecrement operator:
int count;
int index = 6;
count = --index; // count = 5, index = 5
This time the -- operator comes before the expression index, which is why it's called the predecrement operator. index is decremented before the assignment occurs. Because index is decremented first, its value becomes 5, and then the assignment occurs to make the value of count equal 5.
Example: class Program { static void Main(string[] args) { int Value = 2; int preDecrement; int postDecrement; preDecrement = --Value; Console.WriteLine("PreDecrement Value is {0}:", preDecrement); Console.ReadLine(); Console.WriteLine("Value is:{0}", Value); Console.ReadLine(); postDecrement = Value--; Console.WriteLine("PostDecrement Value is {0} :", postDecrement); Console.ReadLine(); Console.WriteLine("Value is:{0}", Value); Console.ReadLine(); } } |
Output:
PreIncrement Value is: 1
Value is:1
PostIncrement Value is:1
Value is:0
Logical Complement Operator:(!)
A logical complement operator (!) serves to invert the result of a Boolean expression. The Boolean expression evaluating to true will be false. Likewise, the Boolean expression evaluating to false will be true.
Example:
class Program { static void Main(string[] args) { int Value = 2; bool logNot; logNot = false; logNot = !logNot; Console.WriteLine("Logical Not: {0}", logNot); Console.ReadLine(); } } |
Output:
Logical Not:True
Bitwise Complement Operator:(~)
A bitwise complement operator (~) inverts the binary representation of an expression. All 1 bits are turned to 0. Likewise, all 0 bits are turned to 1.
Example:
class Program { static void Main(string[] args) { int Value = 2; sbyte bitNot; bitNot = 0; bitNot = (sbyte)(~bitNot); Console.WriteLine("Bitwise Not: {0}", bitNot); Console.ReadLine(); } } |
Output:
Bitwise Not:-1;