Generics in C# with Examples (Classes, Methods, Constraints)

C# generics featured card: a Repository generic class storing Customer records with the real output Customers: 2

Say you’re building the back office for a Phoenix landscaping company. You need in-memory stores for customers, work orders and invoices, and all three classes would be the same 30 lines with a new type name. C# generics fix that: you write Repository<T> once, and the compiler checks every type you plug in. We’ll build … Read more >>

SelectMany in C# Explained with Examples

C# LINQ SelectMany flattening the list of lists Austin, Dallas, Denver, Miami, Tampa into one flat list of five cities

The C# LINQ SelectMany method flattens a sequence of sequences into one sequence, so a list of lists becomes a single list with one call, lists.SelectMany(x => x): Quick answer: use Select when each item maps to one value, and SelectMany when each item holds a collection you want as one flat list. I ran … Read more >>

Dependency Injection in a C# Console App (Generic Host)

Dependency injection in a C# console app: the InvoiceReminder project with a singleton email sender, scoped invoice repository, transient reminder service and options from appsettings.json

Say you’re writing a C# console app for a Columbus, Ohio supply company that emails a reminder for every invoice 14 or more days past due. It needs an email sender and an invoice repository, and neither should be created inside the reminder logic. That’s the job of dependency injection in a C# console app: … Read more >>

Remove Special Characters from a String in C# (Unicode-Safe)

Remove special characters from a string in C#: Zoë O'Neil!! #4521 becomes Zoë ONeil 4521 with the [^p{L}p{N} ] regex

The snippet most people copy to remove special characters from string C# is Regex.Replace(s, “[^a-zA-Z0-9]”, “”). I ran it on a customer name from a Phoenix CRM import, next to the fix: The ASCII pattern deleted the spaces and the accented letters, so José became Jos. The second pattern keeps them. Quick answer: use a … Read more >>

Convert DateTime to a Unix Timestamp (and Back) in C#

C# DateTime to Unix timestamp: 11/27/2026 9:00 AM Eastern becomes 1795788000 with DateTimeOffset.ToUnixTimeSeconds()

To convert a C# DateTime to Unix timestamp, wrap the value in a DateTimeOffset and call ToUnixTimeSeconds(). For the current time, that’s DateTimeOffset.UtcNow.ToUnixTimeSeconds(), a long count of seconds since January 1, 1970 UTC. Going back is just as short with FromUnixTimeSeconds. I ran every method below on .NET 10 against one Black Friday launch time, … Read more >>

Convert UTC to Eastern or Pacific Time in C# (TimeZoneInfo)

Convert UTC to US time zones in C#: 07/04/2026 4:00 PM UTC shown as Eastern, Central, Mountain, Arizona, Pacific, Alaska and Hawaii local time

To convert UTC to Eastern time in C#, look up the zone with TimeZoneInfo.FindSystemTimeZoneById and pass it, with your UTC DateTime, to TimeZoneInfo.ConvertTimeFromUtc: On .NET 10 that turned 6:30 PM UTC on 11/24/2026 into 1:30 PM. Below, I run the same call against every US zone, both 2026 daylight saving switches and the two exceptions … Read more >>

DateTime vs DateTimeOffset vs DateOnly in C#: Which to Use

C# DateTime vs DateTimeOffset card: the same instant at 9:00 AM -08:00 and 12:00 PM -05:00 compares True as DateTimeOffset and False as DateTime

If the same Seattle webinar showed up as two different times in your code, you’ve hit the one difference that matters in C# DateTime vs DateTimeOffset: a DateTimeOffset stores its offset from UTC, and a DateTime doesn’t. Use DateTimeOffset for real instants like orders, logs and meetings, DateOnly for due dates and birthdays, and DateTime … Read more >>

C# DateTime Format: Custom Format Strings with US Examples

C# DateTime format strings MM/dd/yyyy, MMMM d, yyyy, hh:mm tt, dddd and fff with their en-US output for November 23, 2026

For a C# DateTime format like 11/23/2026, call ToString with a custom format string, and pass the en-US culture so the result doesn’t change from one server to the next: Quick answer: MM is the month, mm is minutes, hh needs tt for AM/PM, and “o” is the format for APIs. Every output below comes … Read more >>