Early previewThis guide is in development. Items tagged verify are still being checked against official docs.
nebx Learn
GitHub
Menu
Home/C#/Anti-patterns
C#

C#

A productive, strongly typed language for the whole .NET platform.

Paradigm
OOP, functional features
Typing
Static, strong, nullable-aware
Memory
Generational GC
Runs
.NET runtime (JIT) or Native AOT
Concurrency
Tasks, async/await, channels

Anti-patterns

Solutions that look reasonable but cause problems. First the C#-specific traps, then the classics that apply everywhere.

C#-specific

Anti-pattern
Do this instead
✕ async void methods
✓ Return Task. Exceptions in async void cannot be awaited or caught; keep it for event handlers only.
✕ Blocking on async with .Result or .Wait()
✓ Await the task. Blocking can deadlock in contexts with a synchronization context and starves the thread pool.
✕ catch (Exception) { }
✓ Catch specific exceptions, log, and rethrow with throw; (not throw ex;) to keep the stack trace.
✕ Service locator everywhere
✓ Inject dependencies through constructors so they are visible and testable.
✕ Static mutable state
✓ Use DI-managed singletons; static state leaks between tests and requests.

Universal anti-patterns

God object

One class knows and does too much. Split it by responsibility (SRP).

Spaghetti code

Tangled control flow with no structure. Refactor into small functions with clear inputs and outputs.

Golden hammer

Using one familiar tool or pattern for every problem. Pick the simplest fit.

Premature optimisation

Optimising before measuring. Profile first, then fix the real bottleneck.

Copy-paste programming

Duplicated logic drifts apart. Extract shared code once the duplication is real (rule of three).

Magic numbers and strings

Unexplained literals. Use named constants or enums.

Singleton abuse

Global state disguised as a pattern. Prefer dependency injection with a single-instance lifetime.

Lava flow

Dead or experimental code nobody dares to remove. Delete it; version control remembers.

Saved in this browser only. Part of From code to production (0/5).