> Markdown version of [/videos/1227-tips-tricks-and-quirks-in-net](https://www.wearedevelopers.com/videos/1227-tips-tricks-and-quirks-in-net). Every page supports `.md` or `Accept: text/markdown`. Links point to the HTML versions so they work for humans too. Agent guide: [/agents.md](https://www.wearedevelopers.com/agents.md). --- # Tips, tricks and quirks in .NET Did you know blindly following IDE refactoring suggestions can make your .NET code five times slower? Look beneath the syntax to uncover C#'s most surprising performance quirks. - **Speakers:** Paweł Łukaszuk - **Event:** WeAreDevelopers LIVE - **Published:** October 16, 2024 - **Duration:** 31:13 - **URL:** https://www.wearedevelopers.com/videos/1227-tips-tricks-and-quirks-in-net ## Summary While C# and .NET offer robust capabilities and frequent updates, developers often rely on habitual coding patterns and IDE refactoring suggestions without fully understanding their underlying mechanics. By looking past surface-level syntax and examining intermediate language (IL) code, engineers can uncover surprising performance quirks and implementation details that frequently defy conventional programming wisdom. Assumptions about common data structures and operators often mask underlying inefficiencies. For instance, `for` loops historically executed twice as fast as `foreach` until optimizations were introduced in .NET 7. Similarly, blind reliance on visual tooling can backfire; an IDE's suggestion to condense `.Where(predicate).Count()` into a single `.Count(predicate)` call can actually make code up to five times slower due to the overhead of virtual method dispatch (`callvirt`). String manipulation carries its own myths; while `StringBuilder` is widely considered the default best practice, simple `+` concatenation is often faster for small batches of strings because it avoids the initialization and resizing of underlying arrays. Beyond raw performance, taking a closer look at language quirks exposes critical nuances in safety and logic execution. Simple `== null` checks can produce catastrophic runtime bugs if a class overrides the equality operator, making pattern matching (`is null`) a far safer alternative in modern C#. Deeper system operations feature similar oddities, such as `Environment.FailFast()` completely bypassing `catch` and `finally` blocks to kill a process instantly, or `Math.Round()` utilizing "Banker's Rounding" to pull midpoints toward even numbers rather than traditional school-taught arithmetic. Ultimately, developers are encouraged to go "down into the bowels of your .NET application"—questioning default tooling advice and independently verifying how everyday code actually executes on the machine. **Keywords:** c# performance quirks, .net optimization, linq count performance, foreach loop efficiency, stringbuilder allocation, string concatenation speed, il code analysis, operator overloading risks, is null pattern matching, environment.failfast, banker's rounding, ide refactoring pitfalls, virtual method dispatch overhead ## Chapters 1. **Data types and list implementation in .NET** (00:03) — How the default generic list in .NET is essentially a resizable array with a small default size. 1. **Performance differences between for and foreach loops** (04:21) — Analyzing why for loops historically outperform foreach loops and how .NET 7 optimizes this gap. 1. **Performance comparison of Count and Where in LINQ** (07:41) — Uncovering why filtering collections using Count with a predicate is slower than using Where followed by Count. 1. **StringBuilder versus string concatenation operator performance** (12:00) — Comparing string joining methods to show when standard operator concatenation outperforms StringBuilder operations. 1. **Comparing String.Empty to empty string literals** (16:13) — Examining intermediate code to reveal that String.Empty and empty string literals perform identically. 1. **Operator overloading and null checking vulnerabilities in C#** (18:43) — The risks of relying on legacy equality operators for null checking and how to properly validate objects. 1. **Bypassing try-catch exception handling using Environment.FailFast** (23:31) — Using the Environment.FailFast method to instantly kill an application without triggering standard exception handling. 1. **Understanding banker's rounding behavior in Math.Round** (27:09) — Exploring how the default Math.Round function balances distribution by rounding halves to the nearest even number. ## Related Moments - [Introduction to systems programming and performance in .NET](https://www.wearedevelopers.com/videos/100138-is-it-still-c-practical-systems-programming-with-net-war-stories-included) (from "Is it still C#? Practical systems programming with .NET (war stories included)") - [Dispelling common optimization and runtime performance misconceptions](https://www.wearedevelopers.com/videos/441-c-in-constrained-environments) (from "C++ in constrained environments") - [Evaluating the true impact of bad JavaScript features](https://www.wearedevelopers.com/videos/1451-best-of-the-worst-the-most-awful-anti-features-in-javascript-ranked) (from "Best of the Worst – the most awful anti-features in JavaScript, ranked!") - [Analyzing logic and performance metrics via developer profiling tools](https://www.wearedevelopers.com/videos/859-accelerating-python-on-gpus) (from "Accelerating Python on GPUs") - [Adopting new C# features and tracking updates](https://www.wearedevelopers.com/videos/693-modern-c-a-dive-into-the-community-s-most-loved-new-features) (from "Modern C#: A Dive into the Community's Most Loved new Features.") - [Overview of modern C# feature selections](https://www.wearedevelopers.com/videos/693-modern-c-a-dive-into-the-community-s-most-loved-new-features) (from "Modern C#: A Dive into the Community's Most Loved new Features.") ## Related Articles - [Dev Digest 130 - The Diablo is in the details](https://www.wearedevelopers.com/magazine/470-dev-digest-130-the-diablo-is-in-the-details) - [Dev Digest 127 - putting 5 rings on it](https://www.wearedevelopers.com/magazine/464-dev-digest-127-putting-5-rings-on-it) - [Dev Digest 109 -Egg-citing things…](https://www.wearedevelopers.com/magazine/408-dev-digest-109-egg-citing-things) - [Dev Digest 139 - Soft and hard queries](https://www.wearedevelopers.com/magazine/487-dev-digest-139-soft-and-hard-queries) ## Related Jobs - [Senior Software Engineer](https://www.wearedevelopers.com/jobs/ext/15942-senior-software-engineer) at **GitHub** - [Senior Software Engineer,Billing](https://www.wearedevelopers.com/jobs/ext/1991843-senior-software-engineer-billing) at **GitHub** - [Senior Software Engineer, Fraud](https://www.wearedevelopers.com/jobs/ext/1280398-senior-software-engineer-fraud) at **Twilio** - [Principal Software Engineer, Database Infrastructure](https://www.wearedevelopers.com/jobs/ext/1465908-principal-software-engineer-database-infrastructure) at **GitHub** - [Engineer, Offensive Security Organization](https://www.wearedevelopers.com/jobs/ext/1992296-engineer-offensive-security-organization) at **Twilio** - [Senior Software Engineer, Enterprise Products](https://www.wearedevelopers.com/jobs/ext/1841248-senior-software-engineer-enterprise-products) at **GitHub**