C# & .NET
C# types, async execution and .NET application troubleshooting.
50 troubleshooting guides · Page 2 / 3
Why does checked around a method call fail to catch overflow inside that method?
Overflow-checking context applies to the relevant expressions in its textual scope; it does not rewrite arithmetic hidden inside another method.
Why should a decimal amount use 0.1m rather than a double conversion?
A double literal is a binary floating-point value before a later decimal conversion. Starting with a decimal literal expresses the intended decimal domain directly.
Why does a price parser interpret 1,234 differently across servers?
Default numeric parsing can use the current culture, where comma and period have different roles. Moving the same text between machines can therefore alter its meaning.
Why does DateTime.SpecifyKind change a label without converting the clock value?
SpecifyKind assigns interpretation metadata; it is not a timezone conversion of the represented clock fields. Relabeling local time as UTC can silently shift the intended instant.
Why does adding 24 hours not always mean the same local time tomorrow?
Elapsed duration and calendar-local scheduling are different operations around offset transitions. A fixed offset is not the complete rule set of a named timezone.
Why does a standalone JsonSerializer call ignore camelCase fields that ASP.NET accepts?
Serializer defaults can differ between a standalone options object and web-configured defaults. Assuming all System.Text.Json entry points share the same property matching policy creates inconsistent behavior.
Why are public fields absent from System.Text.Json output?
Default serialization focuses on supported properties rather than automatically including every public field. A DTO using fields can therefore produce less output than expected.
How do I handle parent-child cycles in JSON without accidentally hiding data?
An object graph can contain references that a tree-shaped JSON payload cannot represent directly. Ignoring cycles and preserving references produce different contracts.
Why does an immutable DTO fail constructor binding despite matching JSON names?
Constructor parameter binding follows the serializer’s member-binding rules; a JSON rename alone does not mean any arbitrary parameter name will bind to that member.
Does a required JSON property also reject an explicit null value?
Presence and nullability are separate requirements. A property being required can mean it must appear, while explicit null handling depends on its type, serializer options and validation policy.
Why does an enum serialize as a number instead of its public string name?
Enum representation is a serializer policy. Without the intended converter, numeric output may be valid JSON but incompatible with the consumer’s documented schema.
How do I keep a near-matching regex input from consuming excessive CPU?
Backtracking patterns can take disproportionately long on particular near-matches. Typical short valid inputs do not reveal that risk.
Why can a C# span not remain live across await even when newer syntax allows it nearby?
Ref-like values cannot simply be stored in an async state machine across suspension. Newer C# versions relax some local usage restrictions without turning a span into heap-owned memory.
Why should a protocol identifier comparison specify Ordinal explicitly?
A linguistic comparison can depend on culture in ways unsuitable for machine identifiers. Relying on ambient culture makes behavior change across users or servers.
Why can Path.Combine ignore my intended base directory?
A later rooted path component can replace earlier components rather than remain underneath the base. Combine constructs a path; it is not a containment validator.
Why does reading a MemoryStream immediately after writing return no data?
Writing advances the stream position. A later consumer reading from the current position may start at the end even though the stream contains bytes.
When does a using resource inside a yield iterator get disposed?
Iterator execution is deferred, and resource lifetime follows enumeration rather than merely the method returning an enumerable. A manually abandoned enumerator can keep its resource alive.
Why does cancellation followed by Release cause SemaphoreFullException?
If waiting was canceled before acquisition, the code owns no semaphore permit to release. An unconditional finally around both waiting and work can over-release.
Why does a long-lived publisher keep a closed view model alive?
An event subscription can retain the subscriber through its delegate. Closing the view does not automatically remove a subscription from a longer-lived publisher.
Why can GetOrAdd run my expensive factory more than once for one key?
Dictionary insertion is coordinated, but the value factory can run more than once under contention. Only one produced value may be retained, so a factory with external side effects is hazardous.
Why does Lazy<T> keep throwing after the first initialization failure?
Some Lazy initialization modes cache the factory exception. Re-reading Value can therefore repeat the same failure rather than retrying initialization.
Why does IOptionsSnapshot fail when injected into a singleton service?
Snapshot options have a scoped lifetime, while the singleton outlives that scope. A registration that seems convenient can capture an invalid lifetime or freeze values unintentionally.
Why does authorization fail when authentication middleware runs too late?
Middleware order controls what request state exists when later components execute. Authorization cannot rely on a principal that authentication has not established yet.
Why does changing a status code after writing the body throw a response-started error?
Once headers have been committed, the response status and headers cannot be rewritten as though nothing was sent. A late exception handler may be too late to replace the response.
Ask your own question · Sign in to post questions and answers.