Roadmap › 1. .NET › Step 7
C#

C# Language — Basics to Advanced

Types, control flow, collections, generics, delegates, async/await, memory and modern C# features up to C# 14.
Quick revision for interview day. Print it or save it as a PDF.

Types at a glance

CategoryTypes
Integralbyte short int (32-bit) long (64-bit), unsigned variants
Floatingfloat (~7 digits), double (~15–16 digits), decimal (28–29 digits, use for money)
Othersbool, char (UTF-16), string, object, DateTime, DateOnly, TimeOnly, Guid
User-definedclass, struct, record, record struct, interface, enum, delegate

Value types are copied on assignment; reference types share one object.

Keywords people mix up

  • const vs readonly: compile-time and copied into callers vs set once at runtime.
  • is vs as: is tests a type/pattern (bool); as casts or gives null.
  • ref vs out vs in: read/write vs must assign vs read-only.
  • abstract vs virtual: no body, must override vs has a body, may override.
  • sealed: can't inherit (class) or override further (method).
  • new modifier: hides a base member (no polymorphism).
  • static class: can't be instantiated; only static members.
  • throw; keeps the stack trace; throw ex; loses it.

Null handling

csharp
x?.Prop          // null if x is null
x ?? fallback    // fallback if null
x ??= value      // assign if null
x!               // "trust me, not null"
x is null / x is not null
ArgumentNullException.ThrowIfNull(x);

Collections — pick fast

NeedUse
Ordered listList<T>
Key lookupDictionary<K,V>
Unique itemsHashSet<T>
FIFO / LIFOQueue<T> / Stack<T>
Thread-safe mapConcurrentDictionary<K,V>
Producer/consumerChannel<T>
Read-only, read-heavyFrozenDictionary / ImmutableArray

async rules

  • async Task / Task<T>; async void only for event handlers.
  • Never .Result / .Wait() — async all the way.
  • Independent calls: await Task.WhenAll(...).
  • Pass CancellationToken.
  • CPU-bound: Task.Run / Parallel; I/O-bound: just await.
  • Use SemaphoreSlim (not lock) around await.

Memory & disposal

  • GC generations 0/1/2; LOH ≥ 85,000 bytes.
  • using / await using for IDisposable / IAsyncDisposable.
  • Finaliser = last-resort safety net; GC.SuppressFinalize.
  • Leaks: static collections, events not unsubscribed, HttpClient per call, unbounded caches.
  • Low-allocation code: Span<T>, ArrayPool, StringBuilder, stackalloc.

Modern syntax quick list

csharp
record Person(string Name, int Age);              // C# 9
var p2 = p1 with { Age = 31 };
global using System.Text.Json;                     // C# 10
namespace Shop.Api;                                // file-scoped
var json = """{"name": "Divakar"}""";              // raw string (C# 11)
public required string Name { get; init; }         // required (C# 11)
class Svc(IRepo repo) { }                          // primary ctor (C# 12)
int[] xs = [1, 2, ..others];                       // collection expressions (C# 12)
order?.Status = Status.Shipped;                    // C# 14

The 6 answers to have ready

  1. Value vs reference types (with the stack/heap nuance).
  2. string immutability + StringBuilder.
  3. IEnumerable vs IQueryable.
  4. async/await doesn't create threads; no .Result.
  5. IDisposable + using + GC generations.
  6. Delegates vs events.

Data types & conversion

NeedUse
Money, quantitiesdecimal (12.5m)
Scientific / graphicsdouble
User input → numberint.TryParse(s, out var n)
Overflow checkchecked(a + b)
TimestampsUTC: DateTime.UtcNow / DateTimeOffset
Pure dates / timesDateOnly / TimeOnly

Arrays & collections in one look

int[] fixed · int[,] rectangular · int[][] jagged · List<T> growable · Dictionary<K,V> key lookup (TryGetValue) · HashSet<T> unique · SortedList/SortedDictionary ordered keys · Stack<T> LIFO · Queue<T> FIFO · PriorityQueue<T,P> · ArrayList/Hashtable = legacy (boxing, no type safety) · (int, string) ValueTuple for quick multi-returns.

Delegates, exceptions & files

Func returns · Action void · Predicate bool · += multicast · out T covariance, in T contravariance · throw; not throw ex; · custom exception = derive Exception + 3 constructors · File.ReadLines for big files · always using/await using streams · Path.Combine for paths.