Start Debugging

dotnet

495 posts · Page 2 of 50

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.

Why is the first limiter in PartitionedRateLimiter.CreateChained called twice per request?

ChainedPartitionedRateLimiter walks each limiter once, but the ASP.NET Core rate limiting middleware calls the chain twice when the first pass fails: AttemptAcquire, then AcquireAsync. With fixed window or token bucket limiters a rejected request burns two permits from the first limiter, and a queued request does too. Reproduced on ASP.NET Core 10.0.10 and 11.0 RC3, with a wrapper that makes the middleware walk the chain once.

When is Stopwatch not high resolution in .NET, and what do IsHighResolution and Frequency report on each OS?

On .NET Core and .NET 5+ Stopwatch.IsHighResolution is a hard-coded true and never false. Frequency is whatever QueryPerformanceFrequency says on Windows (usually 10 MHz) and a fixed 1,000,000,000 on Linux, macOS, iOS, Android and WASM, which is a unit, not a resolution. Only .NET Framework can fall back to DateTime. Source walkthrough plus a measured run on .NET 10 / Apple M4 showing 1 ns nominal ticks but 41.67 ns real steps.
Prev Next