C# Cheatsheet
This page is a quick reference for C# patterns that commonly appear in DSA, competitive programming, and technical interviews.
Video Explanation

Basic Syntax
Hello World
using System;
class Program
{
static void Main()
{
Console.WriteLine("Hello World");
}
}
Data Types
int a = 10;
long b = 10000000000L;
double d = 3.14;
float f = 2.5f;
char c = 'A';
bool flag = true;
string s = "Hello";
Operators and Control Flow
if (a > 0)
{
Console.WriteLine("Positive");
}
else if (a == 0)
{
Console.WriteLine("Zero");
}
else
{
Console.WriteLine("Negative");
}
for (int i = 0; i < 5; i++)
{
Console.WriteLine(i);
}
while (a > 0)
{
a--;
}
Arrays
Array Declaration
int[] arr = new int[5];
int[] nums = {1, 2, 3, 4, 5};
Console.WriteLine(arr.Length);
Multidimensional Array
int[,] grid = new int[3,3];
grid[0,0] = 10;
Strings
Common String Operations
string s = "hello";
int len = s.Length;
string upper = s.ToUpper();
bool contains = s.Contains("ell");
string sub = s.Substring(1, 3);
string replaced = s.Replace("h", "H");
StringBuilder
using System.Text;
StringBuilder sb = new StringBuilder();
sb.Append("Hello");
sb.Append(" World");
string result = sb.ToString();
Collections
List
using System.Collections.Generic;
List<int> list = new List<int>();
list.Add(10);
list.Add(20);
int first = list[0];
list.Remove(10);
int size = list.Count;
Dictionary
Dictionary<string, int> map =
new Dictionary<string, int>();
map["Alice"] = 95;
if (map.ContainsKey("Alice"))
{
Console.WriteLine(map["Alice"]);
}
HashSet
HashSet<int> set = new HashSet<int>();
set.Add(1);
set.Add(2);
bool exists = set.Contains(1);
set.Remove(2);
Queue
Queue<int> q = new Queue<int>();
q.Enqueue(10);
q.Enqueue(20);
int front = q.Dequeue();
Stack
Stack<int> st = new Stack<int>();
st.Push(1);
st.Push(2);
int top = st.Pop();
Sorting
Array Sort
int[] arr = {5, 2, 8, 1};
Array.Sort(arr);
Custom Sort
Array.Sort(arr, (a, b) => b.CompareTo(a));
LINQ
Filtering
using System.Linq;
List<int> nums =
new List<int>() {1,2,3,4,5};
var even =
nums.Where(x => x % 2 == 0);
foreach(var x in even)
{
Console.WriteLine(x);
}
Aggregation
int sum = nums.Sum();
int max = nums.Max();
int min = nums.Min();
double avg = nums.Average();
Classes and OOP
Class
class Person
{
public string Name { get; set; }
public Person(string name)
{
Name = name;
}
public void Greet()
{
Console.WriteLine($"Hello {Name}");
}
}
Person p = new Person("John");
p.Greet();
Inheritance
class Animal
{
public virtual void Speak()
{
Console.WriteLine("Animal");
}
}
class Dog : Animal
{
public override void Speak()
{
Console.WriteLine("Bark");
}
}