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
| Category | Types |
|---|---|
| Integral | byte short int (32-bit) long (64-bit), unsigned variants |
| Floating | float (~7 digits), double (~15–16 digits), decimal (28–29 digits, use for money) |
| Others | bool, char (UTF-16), string, object, DateTime, DateOnly, TimeOnly, Guid |
| User-defined | class, struct, record, record struct, interface, enum, delegate |
Value types are copied on assignment; reference types share one object.
Keywords people mix up
constvsreadonly: compile-time and copied into callers vs set once at runtime.isvsas:istests a type/pattern (bool);ascasts or givesnull.refvsoutvsin: read/write vs must assign vs read-only.abstractvsvirtual: no body, must override vs has a body, may override.sealed: can't inherit (class) or override further (method).newmodifier: hides a base member (no polymorphism).staticclass: 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
| Need | Use |
|---|---|
| Ordered list | List<T> |
| Key lookup | Dictionary<K,V> |
| Unique items | HashSet<T> |
| FIFO / LIFO | Queue<T> / Stack<T> |
| Thread-safe map | ConcurrentDictionary<K,V> |
| Producer/consumer | Channel<T> |
| Read-only, read-heavy | FrozenDictionary / ImmutableArray |
async rules
async Task/Task<T>;async voidonly 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: justawait. - Use
SemaphoreSlim(notlock) aroundawait.
Memory & disposal
- GC generations 0/1/2; LOH ≥ 85,000 bytes.
using/await usingforIDisposable/IAsyncDisposable.- Finaliser = last-resort safety net;
GC.SuppressFinalize. - Leaks: static collections, events not unsubscribed,
HttpClientper 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# 14The 6 answers to have ready
- Value vs reference types (with the stack/heap nuance).
stringimmutability +StringBuilder.IEnumerablevsIQueryable.- async/await doesn't create threads; no
.Result. IDisposable+using+ GC generations.- Delegates vs events.
Data types & conversion
| Need | Use |
|---|---|
| Money, quantities | decimal (12.5m) |
| Scientific / graphics | double |
| User input → number | int.TryParse(s, out var n) |
| Overflow check | checked(a + b) |
| Timestamps | UTC: DateTime.UtcNow / DateTimeOffset |
| Pure dates / times | DateOnly / 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.