C# — Operators
Arithmetic operators
using System;
int a = 10, b = 3;
Console.WriteLine($"Add: {a + b}"); // 13
Console.WriteLine($"Sub: {a - b}"); // 7
Console.WriteLine($"Mul: {a * b}"); // 30
Console.WriteLine($"Div: {a / b}"); // 3 (integer)
Console.WriteLine($"Mod: {a % b}"); // 1
Increment and decrement
using System;
int x = 5;
Console.WriteLine(x++); // 5 (post-increment)
Console.WriteLine(++x); // 7 (pre-increment)
Console.WriteLine(x--); // 7 (post-decrement)
Console.WriteLine(--x); // 5 (pre-decrement)
Relational operators
using System;
Console.WriteLine(5 == 5); // True
Console.WriteLine(5 != 3); // True
Console.WriteLine(5 > 3); // True
Console.WriteLine(5 < 3); // False
Console.WriteLine(5 >= 5); // True
Console.WriteLine(5 <= 3); // False
Logical operators
using System;
bool a = true, b = false;
Console.WriteLine(a && b); // False (AND)
Console.WriteLine(a || b); // True (OR)
Console.WriteLine(!a); // False (NOT)
Null-coalescing operators
using System;
string? name = null;
// ?? operator
string displayName = name ?? "Anonymous";
Console.WriteLine(displayName); // Anonymous
// ??= operator (C# 8+)
name ??= "Default";
Console.WriteLine(name); // Default
// ?. operator (null-conditional)
string? text = "Hello";
int? length = text?.Length;
Console.WriteLine(length); // 5
// ?.[] operator
string[]? arr = null;
char? first = arr?[0];
Console.WriteLine(first); // null
Pattern matching
using System;
// is pattern
object obj = 42;
if (obj is int number)
{
Console.WriteLine($"It's an int: {number}");
}
// switch pattern
string Describe(object obj) => obj switch
{
int i when i > 0 => $"Positive int: {i}",
int i => $"Non-positive int: {i}",
string s => $"String: {s}",
null => "Null",
_ => "Unknown"
};
Console.WriteLine(Describe(42));
Console.WriteLine(Describe("hello"));
Ternary operator
using System;
int age = 20;
string status = (age >= 18) ? "Adult" : "Minor";
Console.WriteLine(status); // Adult
String operators
using System;
// Concatenation
string first = "Hello";
string second = "World";
string result = first + " " + second;
Console.WriteLine(result); // Hello World
// Interpolation
string name = "Alice";
Console.WriteLine($"Hello, {name}!");
// Repetition (C# has no * operator for strings)
string line = new string('-', 20);
Console.WriteLine(line); // --------------------
typeof and sizeof
using System;
Console.WriteLine(typeof(int)); // System.Int32
Console.WriteLine(typeof(string)); // System.String
Console.WriteLine(sizeof(int)); // 4
Console.WriteLine(sizeof(double)); // 8
is operator
using System;
Console.WriteLine(42 is int); // True
Console.WriteLine("hi" is string); // True
Console.WriteLine(3.14 is double); // True
// Type pattern
if (42 is int n)
{
Console.WriteLine($"Matched: {n}");
}
Best practices
- Use
?.and??for null-safe code - Prefer pattern matching over type casting
- Use
isfor type checking, notGetType() == - Always use parentheses for clarity in complex expressions
Mini Practice
Write C# code that:
- Uses null-coalescing and null-conditional operators
- Demonstrates pattern matching with
switch - Shows the difference between
==andis - Uses string interpolation with format specifiers
Up Next
In the next lesson, you'll learn about Strings — working with System.String in C#.
Related Topics
Frequently Asked Questions about Operators
What is Operators in C#?
Operators is a fundamental concept in C#. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Operators?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Operators.
Why is Operators important in C#?
Operators is essential for C# development. Understanding this concept will help you write better code and solve real-world problems more effectively.