C# Tutorial
C# String
C# Array
C# Flow Control
C# Class and Object
C# Inheritance
C# Interface
C# Collection
C# Generic
C# File I/O
C# Delegate and Event
C# Exception
C# Process and Thread
C# ADO.NET Database Operations
In C#, operators are special symbols that perform specific operations on operands and return a value. They allow you to manipulate data and perform calculations. In this tutorial, we'll cover the following types of operators in C#:
Let's begin!
Arithmetic operators are used to perform basic mathematical operations:
+
: Addition-
: Subtraction*
: Multiplication/
: Division%
: Modulus (remainder)Example:
int a = 10; int b = 3; int sum = a + b; // 13 int difference = a - b; // 7 int product = a * b; // 30 int quotient = a / b; // 3 int remainder = a % b; // 1
Relational operators are used to compare values and return a boolean result:
==
: Equal to!=
: Not equal to<
: Less than>
: Greater than<=
: Less than or equal to>=
: Greater than or equal toExample:
int a = 10; int b = 3; bool isEqual = a == b; // false bool isNotEqual = a != b; // true bool isLessThan = a < b; // false bool isGreaterThan = a > b; // true bool isLessThanOrEqual = a <= b; // false bool isGreaterThanOrEqual = a >= b; // true
Logical operators are used to combine boolean expressions:
&&
: Logical AND||
: Logical OR!
: Logical NOTExample:
bool a = true; bool b = false; bool andResult = a && b; // false bool orResult = a || b; // true bool notResult = !a; // false
Bitwise operators perform operations on the individual bits of integer operands:
&
: Bitwise AND|
: Bitwise OR^
: Bitwise XOR~
: Bitwise NOT (complement)<<
: Left shift>>
: Right shiftExample:
int a = 5; // 0101 in binary int b = 3; // 0011 in binary int andResult = a & b; // 1 (0001 in binary) int orResult = a | b; // 7 (0111 in binary) int xorResult = a ^ b; // 6 (0110 in binary) int notResult = ~a; // -6 (in two's complement representation) int leftShiftResult = a << 1; // 10 (1010 in binary) int rightShiftResult = a >> 1; // 2 (0010 in binary)
Assignment operators are used to assign a value to a variable:
=
: Assign+=
: Add and assign-=
: Subtract and assign*=
: Multiply and assign/=
: Divide and assign%=
: Modulus and assign&=
: Bitwise AND and assign|=
: Bitwise OR and assign^=
: Bitwise XOR and assignTypes of operators in C#
Operators in C# are classified into various types, including arithmetic, relational, logical, bitwise, assignment, and more.
Arithmetic operators in C#
Arithmetic operators perform basic mathematical operations.
int result = 10 + 5; // Addition
Relational operators in C#
Relational operators compare values and return a boolean result.
bool isGreaterThan = 10 > 5; // Greater than
Logical operators in C#
Logical operators perform logical operations and return a boolean result.
bool logicalResult = true && false; // Logical AND
Bitwise operators in C#
Bitwise operators perform operations at the bit level.
int bitwiseResult = 5 & 3; // Bitwise AND
Assignment operators in C#
Assignment operators assign values to variables.
int x = 10;
Increment and decrement operators in C#
Increment and decrement operators modify the value of a variable.
int x = 5; x++; // Increment
Ternary conditional operator in C#
The ternary conditional operator is a concise way to express conditional statements.
int result = (x > 0) ? 1 : -1;
Null-coalescing operator in C#
The null-coalescing operator provides a default value if an expression is null.
int? nullableValue = null; int result = nullableValue ?? 0;
Overloading operators in C#
Overloading operators allows custom behavior for user-defined types.
public static MyClass operator +(MyClass a, MyClass b) { // Custom addition logic }
Custom operators in C#
Custom operators can be defined for user-defined types to perform specific operations.
public static MyClass operator +(MyClass a, MyClass b) { // Custom addition logic }
Operator precedence in C#
Operator precedence determines the order in which operators are evaluated in an expression.
int result = 2 + 3 * 4; // Multiplication has higher precedence
Operator overloading vs. method overloading in C#
Operator Overloading:
public static MyClass operator +(MyClass a, MyClass b) { // Custom addition logic }
Method Overloading:
public int Add(int a, int b) { // Standard addition logic } public string Add(string a, string b) { // Concatenation logic }