Category: c#


  • Unleashing the Power of BeginScope in .NET

    We have been using the ILogger interface provided by .NET Core, but do you know how to leverage BeginScope for our benefit? Let’s explore how BeginScope helps us harness log scoping effectively. Let’s examine a sample transaction API in a minimal API project and see how one would typically write logs as the transaction flows…

  • Unlock C# 14 Extension Members

    C# 14 introduces a fresh approach to extension methods with extension members. These allow us to group related extensions in a more natural and organized way. One small but important detail: extension declarations must live inside non-generic, non-nested static classes. Additionally, extension is now a reserved keyword, so you can no longer name a type…

  • How Writer Monad Supercharges Option Monad

    We’ll revisit the same example from our previous article on the Option<T> monadโ€”a clean way to handle potential failures without drowning in null checks. By now, you’ve seen how the Option<T> monad lets us chain operations smoothly, sidestepping those tedious null inspections at every step. It’s like assembling a puzzle where missing pieces gracefully halt…

  • Unlock the Differences: When to Use SingleOrDefault vs FirstOrDefault in LINQ

    When to Use SingleOrDefault vs FirstOrDefault? Many developers are familiar with the FirstOrDefault() method in LINQ for C#. However, there’s also SingleOrDefault(). What’s the key difference between them? And why might you choose one over the other instead of always sticking with FirstOrDefault()? Both methods work on collections that implement the IEnumerable<T> interface, allowing you…

  • The only thing you need to reduce nulls: Monad

    Let’s take an example of calculating the delivery estimate given a username. Letโ€™s walk through that journey together and see how a mysterious functional programming concept called a monad can turn nightmare-level null-checks spaghetti into clean, fluent, and safe code. Note: The code snippets here are simplified for clarity and flowโ€”they might not compile straight…

  • Centralizing Package Management in .NET

    If you’re working with just a single project in your solution, this article might not apply to youโ€”feel free to skip it. Thanks for stopping by! Why Do You Need Directory.Packages.props? Imagine you’re building a large .NET solution with dozens of projects: web APIs, class libraries, worker services, and more. Updating a shared NuGet packageโ€”like…

  • A Better Way to Inject Configuration in .NET

    Hey there, fellow .NET developers! If you’ve been building apps for a while, you’re probably no stranger to grabbing settings from appsettings.json using the IConfiguration interface. It’s straightforward and gets the job done. But let’s be realโ€”it’s not without its quirks. Imagine you’re in the middle of a late-night coding session, and a sneaky typo…

  • Uncovering Hidden C# Features: CallerMemberName and CallerLineNumber

    Letโ€™s say you have a utility function thatโ€™s used by multiple other functions to โ€œdo the thing,โ€ and you want to log or print which function actually called this utility. How would you do that? For example, consider a function that extracts the user ID from a JWT token: Now, we want to log who…