In C#, operators are special symbols that perform operations on variables and values. These operations can be mathematical, logical, or relational. Operators are fundamental in programming as they allow you to manipulate data and control the flow of a program.
This tutorial will explore the different types of operators in C# and provide examples to help you understand how to use them.
1. Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations.
List of Arithmetic Operators:
+
(Addition)-
(Subtraction)*
(Multiplication)/
(Division)%
(Modulo or Remainder)
Example:
using System;
class Program
{
static void Main()
{
int a = 10, b = 5;
// Addition
Console.WriteLine("a + b = " + (a + b)); // 10 + 5 = 15
// Subtraction
Console.WriteLine("a - b = " + (a - b)); // 10 - 5 = 5
// Multiplication
Console.WriteLine("a * b = " + (a * b)); // 10 * 5 = 50
// Division
Console.WriteLine("a / b = " + (a / b)); // 10 / 5 = 2
// Modulo
Console.WriteLine("a % b = " + (a % b)); // 10 % 5 = 0
}
}
Output:
a + b = 15
a - b = 5
a * b = 50
a / b = 2
a % b = 0
In this example:
- The
+
,-
,*
,/
, and%
operators perform basic arithmetic operations.
2. Comparison (Relational) Operators
Comparison operators are used to compare two values. They return a boolean value (true
or false
).
List of Comparison Operators:
==
(Equal to)!=
(Not equal to)>
(Greater than)<
(Less than)>=
(Greater than or equal to)<=
(Less than or equal to)
Example:
using System;
class Program
{
static void Main()
{
int a = 10, b = 5;
// Equal to
Console.WriteLine(a == b); // false
// Not equal to
Console.WriteLine(a != b); // true
// Greater than
Console.WriteLine(a > b); // true
// Less than
Console.WriteLine(a < b); // false
// Greater than or equal to
Console.WriteLine(a >= b); // true
// Less than or equal to
Console.WriteLine(a <= b); // false
}
}
Output:
False
True
True
False
True
False
In this example:
- The comparison operators compare the values of
a
andb
, returning boolean results.
3. Logical Operators
Logical operators are used to perform logical operations, typically with boolean values.
List of Logical Operators:
&&
(Logical AND)||
(Logical OR)!
(Logical NOT)
Example:
using System;
class Program
{
static void Main()
{
bool a = true, b = false;
// Logical AND (&&)
Console.WriteLine(a && b); // false
// Logical OR (||)
Console.WriteLine(a || b); // true
// Logical NOT (!)
Console.WriteLine(!a); // false
}
}
Output:
False
True
False
In this example:
&&
returnstrue
only if both conditions aretrue
.||
returnstrue
if at least one condition istrue
.!
negates the value of the condition.
4. Assignment Operators
Assignment operators are used to assign values to variables.
List of Assignment Operators:
=
(Simple assignment)+=
(Add and assign)-=
(Subtract and assign)*=
(Multiply and assign)/=
(Divide and assign)%=
(Modulo and assign)
Example:
using System;
class Program
{
static void Main()
{
int a = 10;
// Simple assignment
a = 5;
Console.WriteLine(a); // 5
// Add and assign
a += 3; // a = a + 3
Console.WriteLine(a); // 8
// Subtract and assign
a -= 2; // a = a - 2
Console.WriteLine(a); // 6
// Multiply and assign
a *= 2; // a = a * 2
Console.WriteLine(a); // 12
// Divide and assign
a /= 4; // a = a / 4
Console.WriteLine(a); // 3
// Modulo and assign
a %= 2; // a = a % 2
Console.WriteLine(a); // 1
}
}
Output:
5
8
6
12
3
1
In this example:
- The assignment operators allow modifying the value of
a
using shorthand notation.
5. Increment and Decrement Operators
The increment (++
) and decrement (--
) operators are used to increase or decrease the value of a variable by one.
List of Increment/Decrement Operators:
++
(Increment)--
(Decrement)
Example:
using System;
class Program
{
static void Main()
{
int a = 5;
// Pre-increment
Console.WriteLine(++a); // 6
// Post-increment
Console.WriteLine(a++); // 6 (then a becomes 7)
// Pre-decrement
Console.WriteLine(--a); // 6
// Post-decrement
Console.WriteLine(a--); // 6 (then a becomes 5)
}
}
Output:
6
6
6
6
In this example:
++a
incrementsa
before the value is used in the expression (pre-increment).a++
incrementsa
after the value is used in the expression (post-increment).--a
anda--
work similarly for decrementing.
6. Ternary Operator
The ternary operator is a shorthand for an if-else
statement. It has the following syntax:
condition ? value_if_true : value_if_false;
Example:
using System;
class Program
{
static void Main()
{
int a = 5, b = 10;
// Ternary operator to find the greater of two numbers
string result = (a > b) ? "a is greater" : "b is greater";
Console.WriteLine(result); // b is greater
}
}
Output:
b is greater
In this example:
- The ternary operator checks the condition
(a > b)
and returns either"a is greater"
or"b is greater"
based on the result.
7. Bitwise Operators
Bitwise operators work on bits and perform bit-by-bit operations. They are used for manipulating data at the binary level.
List of Bitwise Operators:
&
(Bitwise AND)|
(Bitwise OR)^
(Bitwise XOR)~
(Bitwise NOT)<<
(Left shift)>>
(Right shift)
Example:
using System;
class Program
{
static void Main()
{
int a = 5, b = 3;
// Bitwise AND
Console.WriteLine(a & b); // 1
// Bitwise OR
Console.WriteLine(a | b); // 7
// Bitwise XOR
Console.WriteLine(a ^ b); // 6
// Bitwise NOT
Console.WriteLine(~a); // -6
// Left shift
Console.WriteLine(a << 1); // 10
// Right shift
Console.WriteLine(a >> 1); // 2
}
}
Output:
1
7
6
-6
10
2
In this example:
- The bitwise operators perform operations directly on the binary representations of
a
andb
.
Conclusion
C# provides a wide variety of operators that can be used to perform operations on variables and values. These operators include arithmetic, comparison, logical, assignment, and bitwise operators, as well as the ternary operator and increment/decrement operators. Understanding how and when to use these operators is essential for writing efficient and effective code in C#.