C# Cheatsheet

Control Flow and Methods

Use this C# reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.

if / else

if (score >= 90) {
    grade = "A";
} else if (score >= 80) {
    grade = "B";
} else {
    grade = "retry";
}

Conditions must be bool; C# does not treat numbers as truthy or falsy.

switch Statement and Expression

switch (command) {
    case "run":
        Run();
        break;
    default:
        Help();
        break;
}

string dayType = day switch {
    "Sat" or "Sun" => "weekend",
    _ => "weekday"
};

Prefer switch expressions when you are computing one value from one input.

Loops

for (int i = 0; i < 10; i++) { }

while (queue.Count > 0) { }

do { ReadOnce(); } while (NeedsMore());

foreach (string name in names) {
    Console.WriteLine(name);
}

foreach (var (word, index) in names.Select((w, i) => (w, i))) {
    Console.WriteLine($"{index}: {word}");
}

Use break to leave a loop and continue to skip to the next iteration.

Methods

static int Add(int a, int b)
{
    return a + b;
}

static int Square(int n) => n * n;  // expression-bodied

static void Print(string message)
{
    Console.WriteLine(message);
}

A method signature includes name, parameter types, and return type. void means no return value.

Optional, Named, out, and ref Parameters

static string Greet(string name = "student") => $"Hi, {name}";
Greet(name: "Ada");

static bool TryDouble(string text, out int doubled)
{
    if (int.TryParse(text, out int n)) {
        doubled = n * 2;
        return true;
    }
    doubled = 0;
    return false;
}

static void Increment(ref int n) => n++;

Use out for values produced by a method, and ref when the method intentionally mutates an existing variable.

Tuples and Deconstruction

static (int Min, int Max) MinMax(IEnumerable<int> values)
{
    int min = int.MaxValue, max = int.MinValue;
    foreach (int v in values)
    {
        if (v < min) min = v;
        if (v > max) max = v;
    }
    return (min, max);
}

var (min, max) = MinMax([3, 1, 4, 1, 5]);
(int a, int b) = (1, 2);
(a, b) = (b, a);  // swap without a temp

Tuples are the idiomatic way to return two or three values; promote to a record when the group earns a name.

Local Functions and params

static long Factorial(int n)
{
    return Go(n, 1);

    static long Go(int k, long acc) => k <= 1 ? acc : Go(k - 1, acc * k);
}

static int SumAll(params int[] values)
{
    int total = 0;
    foreach (int v in values) total += v;
    return total;
}

int total = SumAll(1, 2, 3);        // any argument count
int fromArray = SumAll([4, 5, 6]);  // or pass a collection

// C# 13: params also accepts span and collection interface types
static int SumSpan(params ReadOnlySpan<int> values)
{
    int total = 0;
    foreach (int v in values) total += v;
    return total;
}

A static local function cannot capture enclosing locals, which makes recursion and hot paths allocation-free.