Part 09 of a hands-on C# practice series. Five runnable, heavily commented examples: immutability and interning proven with ReferenceEquals, the +=-in-a-loop disaster measured against StringBuilder, culture formatting traps (the "3,50" → 350 silent corruption, date ambiguity), correct comparing (StringComparison, the Turkish-i bug reproduced live), and regex essentials with [GeneratedRegex] and ReDoS timeouts.
dotnet run # all examples
dotnet run 3 # just one (e.g. culture traps)| # | Topic | Highlights |
|---|---|---|
| 01 | Immutability | methods return new strings, interning, why == compares content |
| 02 | StringBuilder | 20k appends measured, the API tour, when plain strings win |
| 03 | Formatting & culture | specifiers, alignment, "3,50" parsed as 350 in en-US, ISO dates |
| 04 | Comparing | StringComparison everywhere, Turkish-i reproduced, ordinal vs culture sort |
| 05 | Regex | [GeneratedRegex], named groups, MatchEvaluator, ReDoS timeout demo |
- Machines get
InvariantCulture; humans get their own culture — never the ambient default. - Every
Contains/Equals/sort says how it compares (Ordinalfor machine data). - Never
ToUpperto compare —Equals(..., OrdinalIgnoreCase). - String grows in a loop →
StringBuilder; joining a list →string.Join. - Regex that sees user input gets a timeout; JSON/HTML get real parsers.
← Previous: 08 Modern C# 8–12 · Next: 10 Files & JSON →
| Part | Topic |
|---|---|
| 01 | Collections |
| 02 | LINQ |
| 03 | Delegates & events |
| 04 | Async/await |
| 05 | Generics |
| 06 | Types & memory |
| 07 | Exceptions |
| 08 | Modern C# 8–12 |
| 09 | Strings & text (this repo) |
| 10 | Files & JSON |
| 11 | MiniShop microservices (finale) |
Matej Zlatunić
Released under the MIT License. Feel free to use it for learning.