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.
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.
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.
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
1. What does the `var` keyword actually do in C#?
2. Which syntax represents a nullable value type?
3. What does `catch (HttpRequestException ex) when (condition)` do if condition evaluates to false?
4. Which collection type provides average O(1) lookup for uniqueness checks?
5. In a switch expression, what does the arm `>= 80 and < 90 => "B"` illustrate?
Was this page helpful?
You May Also Like
Variables and Data Types
Covers how C# declares and initializes variables, its built-in value and reference types, type inference with var, and the difference between static and dynamic typing.
Switch Statements and Expressions
Learn how C# branches on a value using the classic switch statement and the modern, expression-based switch introduced in C# 8, including pattern matching arms.
LINQ Basics
Language Integrated Query lets you write expressive, type-safe queries directly in C# over collections, databases, and XML using a unified syntax.
C# Interview Questions
A curated set of common C# interview questions and precise answers covering value vs reference types, async/await, LINQ, and object-oriented design.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics