CancellationToken in C#: Cancel Tasks, Timeouts and HttpClient Calls

C# CancellationToken: a CancellationTokenSource with a 200 ms timeout cancels Task.Delay and throws TaskCanceledException

Say a sales manager in Austin starts a five-store revenue export, then clicks Cancel half a second later. Without cancellation, your code keeps querying Phoenix, Seattle and Boston for nobody. A C# CancellationToken fixes that: a CancellationTokenSource raises the request, and every method you hand its token to stops at its next check. Quick answer: … Read more >>

LINQ Join in C#: Inner, Left, GroupJoin and Multiple Keys

LINQ Join in C#: orders joined to customers by Id, showing order 5001 Jenna Morales $249.99, 5003 Jenna Morales $120.00, 5002 Tyler Brooks $89.50 and 5004 Dana Whitfield $412.00

If you’ve tried to match customers to their orders with two nested loops, you’ve probably seen duplicate rows or customers that quietly vanish. A C# LINQ join does that match in one call: Join pairs each customer with every order that shares its key. For customers with no orders, use GroupJoin plus DefaultIfEmpty, or the … Read more >>

Unit Testing in C# with xUnit: A Beginner’s Guide

xUnit C# unit test: Assert.Equal failing with Expected 0, Actual 7.99 because the shipping rule uses > instead of >=

The first xUnit test most C# developers write checks a condition with Assert.True. When it fails, the runner tells you almost nothing about why: Quick answer: the first rule of any xUnit C# tutorial is to compare values with Assert.Equal(expected, actual), so a failure prints both of them. Below, we build a small class library … Read more >>

LINQ GroupBy in C#: Group, Count and Sum a List

LINQ GroupBy in C#: sales grouped by state with a count and a dollar total per group, TX 3 sales $3,400, CA $2,650, NY $2,000

For a C# LINQ group by, call GroupBy with a key selector such as s => s.State. You get one group per key, and each group carries its Key plus its items, so Count() and Sum() work per group: Below, the same eight US sales get grouped by two columns, filtered like SQL HAVING, turned … Read more >>

Nullable Reference Types in C#: Enable Them and Fix the Warnings

C# nullable reference types: a public string Name field raising warning CS8618, fixed by declaring it required string Name

The most common mistake when a team turns on C# nullable reference types is silencing every warning with the ! operator. The build goes quiet, but the app still crashes, because ! only hides the warning: Quick answer: keep <Nullable>enable</Nullable> on, mark values that can be missing as string?, and fix each warning with a … Read more >>

System.Text.Json in C#: Serialize and Deserialize JSON (.NET 10 Examples)

System.Text.Json in C#: JsonSerializer.Serialize turns new Order(1001, 249.99m) into the JSON string from the real .NET 10 run

To turn a C# object into JSON with System.Text.Json, call JsonSerializer.Serialize; to read it back, call JsonSerializer.Deserialize<T>. Both live in the System.Text.Json namespace that ships with .NET: I ran every example below on .NET 10 against one US customer order, including the native AOT default that breaks reflection JSON in file-based apps. The Sample Order … Read more >>

async and await in C# Explained (with Real Examples on .NET 10)

async await in C#: three one-second calls take 3 s sequentially and 1 s with Task.WhenAll, real .NET 10 output

Say you’re building a shipping-quote screen for a Dallas warehouse. It asks UPS, FedEx and USPS for a rate one after another, and each API takes a second, so every quote takes 3 seconds. That’s the job of the async await C# pattern: await frees the thread while a call waits, and Task.WhenAll lets all … Read more >>