Start Debugging

Blog

Page 3 of 114

Why does a C# decimal keep trailing zeros, and how do I remove them?
1.10m prints "1.10" because System.Decimal stores a scale alongside its digits, and arithmetic, parsing, and JSON all preserve it. How the scale is chosen, why 1.1m == 1.10m but their strings differ, three ways to strip trailing zeros (G29, a 0.## pattern, dividing by 1.000...m) and the 1E-05 trap in G29, how to force a fixed scale, and why .NET 11's double to decimal change produces digits no trick removes. Verified on .NET 10.0.14 and .NET 11 RC3.
Why does string.GetHashCode() return a different value every run in .NET?
Since .NET Core, string.GetHashCode() is seeded with a random per-process value (Marvin hashing), so the same string hashes differently in every process. Why the runtime does it, which types are affected (HashCode.Combine, ValueTuple, records with string members), how Dictionary<string, T> quietly avoids the cost, and how to get a stable hash with XxHash3 or FNV-1a, verified on .NET 10.0.10.
Prev Next