Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What are unary operators in C#?
Unary operators in C# are operators that operate on a single operand. They perform various operations such as incrementing, decrementing, negating, or getting the size of a data type.
Complete List of Unary Operators
| Operator | Name | Description |
|---|---|---|
| + | Unary plus | Indicates positive value (rarely used) |
| - | Unary minus | Negates the value |
| ! | Logical NOT | Inverts boolean value |
| ~ | Bitwise complement | Inverts all bits |
| ++ | Increment | Increases value by 1 |
| -- | Decrement | Decreases value by 1 |
| (type) | Cast | Converts to specified type |
| sizeof | Size of | Returns size in bytes |
Using Increment and Decrement Operators
The increment (++) and decrement (--) operators can be used in prefix or postfix form with different behaviors −
using System;
class Program {
static void Main(string[] args) {
int a = 5;
int b = 5;
Console.WriteLine("Original values: a = {0}, b = {1}", a, b);
Console.WriteLine("Pre-increment ++a = {0}", ++a);
Console.WriteLine("Post-increment b++ = {0}", b++);
Console.WriteLine("After operations: a = {0}, b = {1}", a, b);
Console.WriteLine("\nDecrement operations:");
Console.WriteLine("Pre-decrement --a = {0}", --a);
Console.WriteLine("Post-decrement b-- = {0}", b--);
Console.WriteLine("Final values: a = {0}, b = {1}", a, b);
}
}
The output of the above code is −
Original values: a = 5, b = 5 Pre-increment ++a = 6 Post-increment b++ = 5 After operations: a = 6, b = 6 Decrement operations: Pre-decrement --a = 5 Post-decrement b-- = 6 Final values: a = 5, b = 5
Using Logical and Bitwise Unary Operators
using System;
class Program {
static void Main(string[] args) {
bool flag = true;
int number = 5;
Console.WriteLine("Logical NOT operator:");
Console.WriteLine("flag = {0}, !flag = {1}", flag, !flag);
Console.WriteLine("\nBitwise complement operator:");
Console.WriteLine("number = {0}, ~number = {1}", number, ~number);
Console.WriteLine("Binary: {0} -> {1}", Convert.ToString(number, 2), Convert.ToString(~number, 2));
Console.WriteLine("\nUnary minus operator:");
Console.WriteLine("number = {0}, -number = {1}", number, -number);
}
}
The output of the above code is −
Logical NOT operator: flag = True, !flag = False Bitwise complement operator: number = 5, ~number = -6 Binary: 101 -> 11111111111111111111111111111010 Unary minus operator: number = 5, -number = -5
Using sizeof Operator
The sizeof operator returns the size of a data type in bytes. It can only be used with value types and in unsafe contexts for reference types −
using System;
class Program {
static void Main(string[] args) {
Console.WriteLine("Size of various data types:");
Console.WriteLine("The size of int is {0} bytes", sizeof(int));
Console.WriteLine("The size of char is {0} bytes", sizeof(char));
Console.WriteLine("The size of short is {0} bytes", sizeof(short));
Console.WriteLine("The size of long is {0} bytes", sizeof(long));
Console.WriteLine("The size of double is {0} bytes", sizeof(double));
Console.WriteLine("The size of float is {0} bytes", sizeof(float));
Console.WriteLine("The size of bool is {0} bytes", sizeof(bool));
Console.WriteLine("The size of byte is {0} bytes", sizeof(byte));
}
}
The output of the above code is −
Size of various data types: The size of int is 4 bytes The size of char is 2 bytes The size of short is 2 bytes The size of long is 8 bytes The size of double is 8 bytes The size of float is 4 bytes The size of bool is 1 bytes The size of byte is 1 bytes
Conclusion
Unary operators in C# work on single operands and include arithmetic operators (+ -), logical operators (! ~), increment/decrement operators (++ --), and special operators like sizeof. Understanding the difference between prefix and postfix increment/decrement operators is crucial for writing effective C# code.
