dev.to
Top 10 errors found in C# projects in 2025
Excerpt
## 10th place. Try to find it Today's top starts with an error mentioned in the article about checking .NET 9. It feels like .NET 9 was just released, but a little over a month ago, .NET 10 replaced it. ... PVS-Studio warning: V3025 [CWE-685] Incorrect format. A different number of format items is expected while calling 'AppendFormat' function. Format items not used: {3}, {4}. Arguments not used: 1st. VMInstruction.cs 105 Calling the overridden `ToString` method inevitably causes an exception. This is due to an incorrect `sb.AppendFormat` call containing two errors. - The number of arguments to insert is less than the number of placeholders in the format string, which causes the exception. - Even if we fix the first issue by matching the number of arguments and placeholders, the call will still throw the exception. This is because placeholder indexing starts at 0, not 1. This means the fifth argument is required for the placeholder with index 4, which is absent. … PVS-Studio warnings: V3192 The 'Max' property is used in the 'GetHashCode' method but is missing from the 'Equals' method. ScottPlot CoordinateRangeMutable.cs 198 V3001 There are identical sub-expressions 'Equals(Min, other.Min)' to the left and to the right of the '&&' operator. ScottPlot CoordinateRangeMutable.cs 172 The analyzer issued two warnings for this code fragment. ... PVS-Studio warning: V3123 [CWE-783] Perhaps the '??' operator works in a different way than it was expected. Its priority is lower than priority of other operators in its left part. RecoveryMessage.cs 35 The analyzer issued several V3123 warnings for this code, but I've included only one for brevity. The `??`operator has lower precedence than the `+` operator. However, the formatting of this expression suggests developers expected the opposite. … PVS-Studio warning: V3207 [CWE-670] The 'not 0 or 1' logical pattern may not work as expected. The 'not' pattern is matched only to the first expression from the 'or' pattern. Files.App.Controls Omnibar.cs 149 Let's look closer at the `itemCount is not 0 or 1` part. Already guessed what's the issue? This pattern is redundant. Its second part affects nothing. When saying "x is not 0 or 1", people usually imply that x is neither 0 nor 1. However, in C#, operator precedence works differently—`x is not 0 or 1` actually means `x is (not 0) or 1`. Such mistakes can lead not only to redundancy but also to errors like `NullReferenceException: list is not null or list.Count == 0`. … ## 1st place. How does LINQ work? And the winner is an error from an article about checking the Lean trading engine. This error ranks first due to its subtlety. Some developers may not consider the effects of using deferred execution methods in combination with captured variables. All the details are below: … While iterating over the `csv` collection, the `lineNumber` variable is also incremented. As a result, each iteration increases `lineNumber by 2`: when the delegate runs and inside the `foreach`, which looks odd. Note the `lineNumber = 0` assignment before `foreach`. It's likely that developers expected that this variable could hold a non-zero value before the loop. However, that is impossible: `lineNumber` starts at zero, and the only place that changes it before the `foreach` sits in the delegate. As mentioned above, the delegate runs during iteration, not before it. Apparently, the developers expected the delegate to execute prior to entering the loop.
Related Pain Points
Operator precedence surprises in pattern matching and null-coalescing
6The `??` (null-coalescing) operator and pattern matching operators have unintuitive precedence rules. For example, `x is not 0 or 1` evaluates as `(x is not 0) or 1` rather than the intended `x is not (0 or 1)`, leading to subtle bugs and redundant logic.
Frequent breaking changes and rapid major version releases create maintenance burden
6Next.js has introduced 15 major versions in 8 years, each potentially containing backwards-incompatible changes. This creates significant maintenance burdens for long-term projects and makes it difficult for teams to keep applications updated.
Function Closures in For Loops Create Variable Reference Bugs
5Functions defined inside for loops incorrectly capture the loop variable by reference rather than by value, causing all callback functions to reference the final loop value instead of their intended iteration value.