100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
C#

C# Quick Reference

A condensed cheat sheet of core C# syntax, keywords, and idioms — data types, control flow, collections, and modern language features at a glance.

Interview PrepBeginner8 min readJul 9, 2026
Analogies

C# Quick Reference

This reference condenses the syntax and idioms covered across the rest of the course into a single scannable page — useful for a quick refresher before an interview, or as a lookup while coding when you can't quite remember a syntax detail. It's organized by category rather than by learning order: types and control flow, collections and LINQ, and async/exception handling.

🏏

Cricket analogy: Like a laminated cheat-sheet of field placements and DRS rules a captain keeps in his pocket during a match, this page condenses everything scattered across coaching sessions into one quick sideline glance.

Types, Control Flow, and Pattern Matching

C# is statically typed; var lets the compiler infer a variable's type from its initializer without making the variable dynamically typed — the type is still fixed at compile time. Nullable value types are written with a trailing ? (e.g. int?), and with nullable reference types enabled (the default in new projects), reference types are non-nullable by default unless also suffixed with ?. switch supports both a statement form (case/break) and, since C# 8, a concise expression form using => that pattern-matches and returns a value directly, including relational patterns (>= 90) and logical combinators (and, or, not).

🏏

Cricket analogy: Just as a scorer writes 'W' knowing it always means wicket regardless of how it's phrased on the card, var lets the compiler fix a type at compile time even though you didn't spell it out yourself.

csharp
var name = "Ada";               // inferred as string
int? age = null;                 // nullable value type
string? nickname = null;         // nullable reference type
const double Pi = 3.14159;       // compile-time constant

int score = 87;
string grade = score switch
{
    >= 90 => "A",
    >= 80 and < 90 => "B",
    >= 70 and < 80 => "C",
    _ => "F"
};

string? city = customer?.Address?.City;
string displayCity = city ?? "Unknown";

Collections, LINQ, and Async Essentials

List<T> is the default growable collection; Dictionary<TKey, TValue> handles key-value lookups; HashSet<T> gives uniqueness checks with average O(1) lookup. LINQ's most-used operators are Where (filter), Select (project), OrderBy/OrderByDescending (sort), GroupBy (bucket), FirstOrDefault (safe single-item fetch), and Any/All (existence checks). An async method is declared with the async modifier and typically returns Task or Task<T>; within it, await suspends until the awaited operation completes without blocking the calling thread. Exception handling uses try/catch/finally, and catch (ExceptionType ex) when (condition) adds a conditional filter without consuming exceptions that don't match.

🏏

Cricket analogy: A team roster (List<T>) grows as players are added for the tour, a Dictionary maps jersey numbers to player names for instant lookup, and Where filtering the roster for 'bowlers only' mirrors LINQ's Where.

csharp
var numbers = new List<int> { 5, 3, 9, 1, 9, 7 };

var result = numbers
    .Where(n => n > 2)
    .Distinct()
    .OrderByDescending(n => n)
    .Select(n => n * 10)
    .ToList(); // [90, 70, 50, 30]

var lookup = new Dictionary<string, int> { ["a"] = 1, ["b"] = 2 };
if (lookup.TryGetValue("a", out var value))
    Console.WriteLine(value);

Modern C# favors expression-bodied members (public int Square(int x) => x * x;), target-typed new() (List<string> names = new();), and primary constructors on classes (class Person(string name, int age)) to reduce boilerplate — all are safe defaults to reach for in new code targeting C# 12.

csharp
public async Task<string> FetchAsync(HttpClient client, string url)
{
    try
    {
        var response = await client.GetAsync(url);
        response.EnsureSuccessStatusCode();
        return await response.Content.ReadAsStringAsync();
    }
    catch (HttpRequestException ex) when (ex.StatusCode == System.Net.HttpStatusCode.NotFound)
    {
        return string.Empty;
    }
    finally
    {
        Console.WriteLine("Fetch attempt complete.");
    }
}
  • var infers a static type at compile time; it does not make a variable dynamically typed.
  • Nullable value types use ? (int?); nullable reference types also use ? when NRT is enabled.
  • Switch expressions (=>) offer a concise, pattern-matching alternative to switch statements.
  • List<T>, Dictionary<TKey,TValue>, and HashSet<T> cover the vast majority of everyday collection needs.
  • Common LINQ operators: Where, Select, OrderBy, GroupBy, FirstOrDefault, Any/All.
  • catch (ExceptionType ex) when (condition) adds a conditional filter to exception handling without consuming non-matching exceptions.

Practice what you learned

Was this page helpful?

Topics covered

#CStudyNotes#Programming#CQuickReference#Quick#Reference#Types#Control#StudyNotes#SkillVeris#ExamPrep