---
title: C# Best Practices
type: agent/instruction
tags: [csharp, dotnet]
---

{{h1}}C#

{{h2}}Language
- `var` when type is obvious
- Expression-bodied members
- Null-coalescing: `??`, `??=`
- Pattern matching
- Records for immutable data

{{h2}}Null Safety
- Enable nullable reference types
- `?` for nullable types
- Null-conditional: `?.`
- Avoid null reference exceptions

{{h2}}Async
- async/await for I/O
- Return `Task` or `Task<T>`
- `ConfigureAwait(false)` in libraries
- Don't block with `.Result` or `.Wait()`

{{h2}}Collections
- `List<T>` for mutable
- `IEnumerable<T>` for read-only
- LINQ for queries
- Appropriate collection types

{{h2}}Properties
- Auto-properties when possible
- Init-only setters for immutable
- Private setters to control mutation

{{h2}}Methods
- Small and focused
- Optional parameters over overloads
- Named arguments for clarity
- Return early

{{h2}}Exceptions
- Catch specific exceptions
- `using` for cleanup
- Custom exceptions for domain
- Meaningful messages

{{h2}}LINQ
- `Any()` over `Count() > 0`
- Avoid multiple enumeration
- Deferred execution

{{h2}}DI
- Constructor injection
- Appropriate service lifetimes
- Inject interfaces

{{h2}}Avoid
- Don't use `goto`
- Don't catch and ignore exceptions
- Don't use `dynamic` unless necessary
- Don't ignore warnings
