Chaining multiple Replace() calls works.
But when you're cleaning formatted input such as phone numbers, Regex can be a cleaner and more scalable option.
Use Regex when the pattern matters.
#csharp#dotnet#regex
Grouping just to remove duplicates?
Modern C# already has a cleaner solution.
Use DistinctBy() when selecting unique values from a collection.
#csharp#dotnet#cleancode
Avoid building strings manually in loops.
Use string.Join when combining values from a collection.
Cleaner intent.
Less boilerplate.
#csharp#dotnet#cleancode
If you only need the first matching item,
don’t chain Where() before FirstOrDefault().
Pass the predicate directly instead.
Shorter query.
Clearer intent.
#csharp#dotnet#cleancode
First() throws if the collection is empty.
Use FirstOrDefault() and handle the null case explicitly.
Safer code and fewer runtime surprises.
#csharp#dotnet#cleancode
Avoid ternary checks for simple null defaults.
Use the null-coalescing operator (??) instead.
It expresses intent clearly
and reduces unnecessary conditions.
#csharp#dotnet#cleancode
Avoid ToLower-based string comparisons.
They are culture-dependent
and can produce unexpected behavior.
Use string.Equals with StringComparison
for predictable results.
#csharp#dotnet#cleancode
If you already know the values,
don’t build a list with multiple Add calls.
Use a collection initializer instead.
Cleaner structure.
Less repetition.
#csharp#dotnet#cleancode
Avoid manual null checks when calling methods.
Use the null-conditional operator instead.
It executes the method only if the object
is not null — cleaner and easier to read.
#csharp#dotnet#cleancode
Avoid using exceptions for normal validation.
Instead of wrapping Parse in try-catch,
use TryParse.
It communicates intent clearly
and avoids unnecessary exception handling.
#csharp#dotnet#cleancode
Count() > 0 works.
But Any() expresses intent better:
you only care if at least one item exists.
With IEnumerable, Any() may also avoid
enumerating the entire sequence.
Small change.
Better code.
#csharp#dotnet#cleancode
Keep your response body clean.
Return only the data in the body
and move pagination metadata to headers.
Simple.
Readable.
Production-ready.
#csharp#dotnet#webapi
If you are not using the index,
you probably don’t need a for loop.
Prefer foreach when iterating over collections
without relying on position.
Cleaner intent.
Better readability.
#csharp#dotnet#cleancode