DEV Community

Cover image for 7 C# Techniques Pros Use Without Thinking
Sukhpinder Singh
Sukhpinder Singh

Posted on • Originally published at Medium

7 C# Techniques Pros Use Without Thinking

Here are the 7 C# techniques pros use on autopilot in 2026.

1. Pattern Matching So Hard I Forgot How to Write If-Else

I haven’t written a classic if (something != null) chain in forever. My brain just goes straight to:

var result = order switch
{
    { Status: OrderStatus.Paid, Items.Count: > 5 } => "VIP order!",
    { IsOverdue: true } => "Send the angry email",
    _ => "Handle normally"
};
Enter fullscreen mode Exit fullscreen mode

Property patterns, list patterns, relational patterns, the not pattern… it all just flows out. Last week I refactored a 180-line validation monster into 28 beautiful lines during a client call. The client literally said “wait… what just happened?” Felt like a magician.

2. Records + with Expressions = My Daily Immutability Fix

Everything that carries data is a record now. Need to change one property without mutating the original?

var updated = user with { Email = newEmail, LastModified = DateTime.UtcNow };

No more defensive copying, no more “who mutated my object?!” bugs at 2am. My DTOs, events, commands — all records. My tests got shorter, my APIs got safer, and I stopped being scared of passing things around. It’s honestly ridiculous how good this feels once it becomes habit.

3. Span & Memory (My Brain Auto-Reaches for Them)

See a string or array in a loop? My fingers just type ReadOnlySpan<char> or stackalloc before I even realize I’m optimizing.

Parsing logs? Spans. Splitting user input? Spans. Hot path in the payment service? You already know.

I don’t benchmark every little thing anymore — I just instinctively avoid allocations now. The day my Azure bill dropped 18% after one refactor was a very good day.

4. Primary Constructors + required Members (Classes Feel Naked Without Them)

public class OrderService(OrderRepository repo, ILogger<OrderService> logger, IEmailSender email)
{
    public async Task Process(...) { ... }
}
Enter fullscreen mode Exit fullscreen mode

No more private readonly fields + constructor assignment dance. Add required on properties and the compiler yells at me if I forget to set something. I write classes faster, they’re shorter, and I make way fewer dumb mistakes. C# 12/13+ made this feel like cheating with permission.

5. IAsyncEnumerable Streaming — Because Loading Everything Is So 2022

Big dataset? I don’t .ToListAsync() anymore. I yield return async and let the caller consume it lazily:

public async IAsyncEnumerable<Order> GetLargeReportAsync()
{
    await foreach (var batch in _repo.GetBatchesAsync())
        foreach (var order in batch)
            yield return order;
}
Enter fullscreen mode Exit fullscreen mode

My memory usage in reporting jobs went from “oops” to “beautiful.” Feels like I leveled up as a developer the day this became my default.

6. Local Functions + Expression-Bodied Members Everywhere

Big method? I just drop a tiny local function right there instead of polluting the class. Everything else becomes expression-bodied (=>) so my methods look like haiku.

One method, clean main flow on top, details tucked neatly below. Code reviews went from “can you break this down?” to “looks good.” I love it so much I sometimes do it just for fun.

7. Source Generators & Smart Attributes (Boilerplate? Never Heard of Her)

I have private generators that auto-generate mapping code, validation from attributes, even full CRUD endpoints from a single entity class. Plus things like [GeneratedRegex], CallerArgumentExpression, and custom attributes that make error messages actually helpful.

I barely write repetitive code anymore. The generator just… does it. Feels illegal. But Microsoft ships it, so I’m calling it professional.


There you have it — the quiet C# superpowers that make me faster, my code cleaner, and my nights less stressful.

These aren’t the flashy features everyone tweets about. They’re the little habits that compound. The stuff that makes you look like you just “get it” without trying to look smart.

If any of these hit you in the feels, or you have your own “I do this without thinking” C# trick, drop it in the comments. I read every single one (especially the ones that make me go “wait… I need to steal that”).

P.S. Open any file right now and refactor one method using two of these. You’ll feel the difference immediately. Promise. ❤️

Top comments (0)