Primary Constructors in C#: Examples, DI and Inheritance

C# primary constructor: the field assignment _customer = customer; from a classic constructor becomes class Order(string customer)

Say you’re writing checkout code for Ridgeline Outfitters, a gear shop in Boise, Idaho. Every class opens with the same chore: declare private fields, write a constructor, copy each argument across. A C# primary constructor cuts that to one line. Put the parameters right after the class name, like class Order(string customer, decimal subtotal), and … Read more >>

Distinct and DistinctBy in C#: Remove Duplicates from a List

C# LINQ Distinct turning the ZIP code list 78701, 80202, 78701, 12207, 80202 into 78701, 80202, 12207

To remove duplicates from a list, call Distinct() and then ToList(). The C# LINQ Distinct method keeps the first copy of each value and drops the rest: For objects, DistinctBy(s => s.Email) (.NET 6+) removes duplicates by one property. I ran every example on .NET 10 against one webinar sign-up list with three hidden duplicates. … Read more >>

Blazor Component Lifecycle: The Real Call Order in .NET 10

Blazor component lifecycle featured card: a table of the real call order logged by the OrderBoard page, a Static prerender pass then a Server pass

A pizza shop in Chicago’s Wicker Park wanted a live order board in Blazor. My first version hit the database twice on every visit, and its 30-second refresh timer kept firing after the cashier left the page. Both bugs came from the Blazor component lifecycle. Quick answer: load data in OnInitializedAsync with [PersistentState], start timers … Read more >>

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 >>

Minimal API Validation in ASP.NET Core (.NET 10): AddValidation, DataAnnotations and 400 Errors

Minimal API validation in .NET 10: AddValidation() returns 400 with Name and Email errors from DataAnnotations on a record

Your endpoint just saved an order with a blank customer name and the email address tyler.brooks@. Nothing stopped it. Quick answer: minimal API validation is built into .NET 10. Call builder.Services.AddValidation(), put DataAnnotations such as [Required] and [Range] on your request types, and bad requests get a 400 before your handler runs: Below, we build … 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 >>

Global Error Handling in ASP.NET Core with IExceptionHandler and ProblemDetails

ASP.NET Core global exception handling in .NET 10: an IExceptionHandler turns a TimeoutException into an application/problem+json response with status 503

For ASP.NET Core global exception handling, call AddProblemDetails() and app.UseExceptionHandler(), then add an IExceptionHandler class for each kind of error you want to map. Every unhandled exception comes back as a clean application/problem+json response instead of a stack trace or an empty 500. I built a small .NET 10 shipping API with three handlers, broke … Read more >>

Blazor Forms and Validation with EditForm (.NET 10)

Blazor form validation featured card: C# code that runs Validator.TryValidateObject on a Runner model and prints The FullName field is required and an email error

Picture the sign-up page for a Thanksgiving 5K in Austin. Most bad entries last year were blank emails, typos like “jenna.morales@” and kids under the 14+ age limit. Blazor form validation stops all three before your code runs: an EditForm, data annotation attributes on the model and a DataAnnotationsValidator. We’ll build that form on .NET … Read more >>