Operators in C#

Operators in C# are symbols or keywords used to perform operations on values and variables. They are used for arithmetic calculations, assignments, comparisons, logical decisions, bitwise operations, null checks, type checks, object creation, and more. Without operators, a program could store values but could not meaningfully calculate, compare, or control flow.

C# has many operators, but beginners do not need to memorize all of them at once. The important thing is to understand what each category does, how operators interact with data types, and how precedence affects expressions. Operators become especially important when you write conditions, loops, calculations, and method logic.


What Is an Operator in C#?

An operator performs an operation on one or more operands. An operand is the value or variable on which the operator works.

int result = 10 + 5;

Here, + is the operator, and 10 and 5 are operands. The expression produces the value 15.

Operators are the building blocks of expressions in C#.

Types of Operators in C#

Operator CategoryExamplesPurpose
Arithmetic+, -, *, /, %Perform mathematical calculations.
Assignment=, +=, -=Assign or update values.
Comparison==, !=, >, <Compare values.
Logical&&, ||, !Combine or reverse Boolean conditions.
Bitwise&, |, ^, ~Work at the bit level.
Null-related??, ?.Handle possible null values safely.
Conditional?:Choose one value based on a condition.

Arithmetic Operators in C#

Arithmetic operators are used for mathematical operations.

OperatorNameExample
+Additiona + b
-Subtractiona - b
*Multiplicationa * b
/Divisiona / b
%Moduloa % b
int a = 10;
int b = 3;

Console.WriteLine(a + b); // 13
Console.WriteLine(a - b); // 7
Console.WriteLine(a * b); // 30
Console.WriteLine(a / b); // 3
Console.WriteLine(a % b); // 1

When both operands are integers, division produces an integer result. In the example, 10 / 3 gives 3, not 3.333.

Assignment Operators

Assignment operators store or update values in variables.

OperatorMeaning
=Assign value
+=Add and assign
-=Subtract and assign
*=Multiply and assign
/=Divide and assign
%=Modulo and assign
int score = 10;
score += 5;  // score = score + 5
score *= 2;  // score = score * 2

Console.WriteLine(score);

Increment and Decrement Operators

The increment operator increases a value by one, and the decrement operator decreases a value by one.

int count = 5;
count++;
count--;

count++ is commonly used in loops. C# also supports prefix and postfix forms, such as ++count and count++. The difference matters when the expression value is used immediately.

Comparison Operators

Comparison operators compare values and return a Boolean result: true or false.

OperatorMeaning
==Equal to
!=Not equal to
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to
int age = 18;
Console.WriteLine(age >= 18); // true

Comparison operators are heavily used in if statements, loops, validation logic, and filtering conditions.

Logical Operators

Logical operators work with Boolean values and Boolean expressions.

OperatorNameMeaning
&&Logical ANDTrue only when both conditions are true.
||Logical ORTrue when at least one condition is true.
!Logical NOTReverses a Boolean value.
int age = 22;
bool hasId = true;

if (age >= 18 && hasId)
{
    Console.WriteLine("Entry allowed");
}

The condition is true only if the age is at least 18 and hasId is true.

Bitwise Operators

Bitwise operators work on individual bits of integer values. They are less common in beginner application code but important in flags, permissions, low-level work, and performance-sensitive logic.

OperatorMeaning
&Bitwise AND
|Bitwise OR
^Bitwise XOR
~Bitwise complement
<<Left shift
>>Right shift

Conditional Operator

The conditional operator ?: is a compact way to choose one of two values based on a condition.

int marks = 75;
string result = marks >= 40 ? "Pass" : "Fail";

Console.WriteLine(result);

If the condition is true, the first value is selected. If the condition is false, the second value is selected.

Null-Coalescing Operator

The null-coalescing operator ?? provides a fallback value when the left side is null.

string? name = null;
string displayName = name ?? "Guest";

Console.WriteLine(displayName);

Because name is null, displayName becomes Guest. This operator is common in modern C# code that handles optional values.

Null-Conditional Operator

The null-conditional operator ?. safely accesses a member only when the object is not null.

string? message = null;
int? length = message?.Length;

If message is null, the expression returns null instead of throwing a null reference exception.

Type Checking Operators

C# provides operators for checking and converting types safely.

OperatorPurpose
isChecks whether an object matches a type or pattern.
asAttempts a safe reference conversion and returns null if it fails.
typeofGets type information.
nameofGets the name of a variable, type, or member as a string.
object value = "Hello";

if (value is string text)
{
    Console.WriteLine(text.Length);
}

Operator Precedence

Operator precedence decides which operation runs first in an expression. Multiplication and division have higher precedence than addition and subtraction.

int result = 10 + 5 * 2;
Console.WriteLine(result); // 20

The multiplication happens first, so the result is 20, not 30. Parentheses can make the intended order clear.

int result = (10 + 5) * 2;
Console.WriteLine(result); // 30

Unary Operators in C#

Unary operators work with one operand. Examples include unary plus, unary minus, logical NOT, increment, and decrement. They are simple but common in everyday code.

int number = 10;
int negative = -number;
bool isValid = false;
bool opposite = !isValid;

The unary minus changes the sign of the number. The logical NOT operator reverses a Boolean value.

String Concatenation Operator

The + operator is also used to join strings. When one side is a string, C# can combine values into text.

string firstName = "Aman";
string lastName = "Sharma";
string fullName = firstName + " " + lastName;

Console.WriteLine(fullName);

For many modern cases, string interpolation is more readable, but understanding concatenation is still important because it appears in older examples and simple beginner code.

Short-Circuit Evaluation

The logical AND operator && and logical OR operator || use short-circuit evaluation. This means C# may skip checking the second condition when the final result is already known.

string? name = null;

if (name != null && name.Length > 3)
{
    Console.WriteLine("Valid name");
}

If name is null, the second condition is not evaluated. This prevents a null reference problem in this example.

Null-Coalescing Assignment Operator

C# also provides the null-coalescing assignment operator ??=. It assigns a value only when the left side is currently null.

string? title = null;
title ??= "Untitled";

Console.WriteLine(title);

This is useful for default initialization when a value may or may not already exist.

Shift Operators Example

Shift operators move bits left or right. A left shift by one position often multiplies an integer by 2 in simple positive-number cases, while a right shift divides by 2 in similar cases.

int value = 4;
Console.WriteLine(value << 1); // 8
Console.WriteLine(value >> 1); // 2

Shift operators are not needed in most beginner business programs, but they are useful in bit flags, binary data, graphics, embedded-style logic, and performance-focused code.

Assignment vs Comparison

One of the most common beginner mistakes is confusing assignment with comparison. The assignment operator = stores a value. The equality operator == checks whether two values are equal.

int age = 18;          // assignment
bool adult = age == 18; // comparison

In C#, this mistake is often caught by the compiler in conditions, but understanding the difference is still essential for reading and writing correct expressions.

Simple Operator Precedence Table

Higher PriorityExamples
Unary!, ++, --
Multiplicative*, /, %
Additive+, -
Relational>, <, >=, <=
Equality==, !=
Logical AND&&
Logical OR||
Assignment=, +=, -=

You do not need to memorize the entire precedence chart immediately. When an expression becomes hard to read, use parentheses to make the intended order obvious.

Practical Expression Example

In real programs, operators are often combined to form meaningful expressions. For example, an online store may calculate a final price using arithmetic operators and then compare it against a budget using comparison operators. This is where operators become part of actual program logic instead of isolated symbols.

Common Mistakes with Operators in C#

  • Using = when comparison needs ==.
  • Forgetting that integer division removes the decimal part.
  • Writing complex expressions without parentheses.
  • Confusing logical && with bitwise &.
  • Using || when the logic requires &&.
  • Not handling null values safely before member access.
What is the difference between = and == in C#?

= assigns a value to a variable. == compares two values and returns true or false.

What does % mean in C#?

The % operator is the modulo operator. It returns the remainder after division.

Why should I use parentheses in expressions?

Parentheses make the order of operations explicit. They improve readability and prevent mistakes caused by misunderstood precedence.

Best Practices for Operators in C#

  • Use parentheses when expression order may not be obvious.
  • Keep conditions readable instead of writing very long Boolean expressions.
  • Use null-related operators to safely handle optional values.
  • Be careful with integer division.
  • Use meaningful variable names so expressions explain themselves.
  • Prefer clarity over clever operator-heavy code.

Operators in C# make expressions powerful. They let your code calculate values, compare data, make decisions, handle nulls, and update variables. Once you understand operator categories and precedence, writing conditions and calculations becomes much easier.