
Max(-1, 500) = 500 and other small C# bugs
Five small .NET 10 bugs, each reproduced and then fixed: an unlimited quota lost to Max, a JSON redactor that fails only on arrays, a minimal API parameter inferred as a body, Uri.ToString() on a SAS link, and two names the compiler and analyzers reject.
In my projects I work with coding agents such as Claude Code, and of course the code still has the same small bugs that any .NET developer meets. In this article I want to go through five of them, because each one is only a few lines of code, although two of them had a real cost. One made tracking calls return 500 for about 5 days, and another one left two merged PRs undeployed. All the code below was run on .NET 10 (runtime 10.0.10), and for each bug the old behaviour comes first and then the fix.
When -1 means unlimited
A quota can have several contributions, and a very natural rule is that the most permissive contribution should win. In one of my projects this rule was implemented with Max, which looks completely correct and is correct as long as all the values are normal numbers.
public static int MostPermissiveQuota(IEnumerable<int> limits) => limits.Max();The problem is that -1 meant unlimited in this system, so the most permissive value was also the smallest number in the list. As a result, Max(-1, 500) returns 500, and an unlimited quota becomes a limit of 500 without any error. That's because Max knows only about numbers, and the meaning of -1 exists only in our own convention.
The fix is that any negative contribution wins, regardless of its position in the list:
public const int Unlimited = -1;
public static int MostPermissiveQuota(IEnumerable<int> limits)
{
var highest = 0;
foreach (var limit in limits)
{
if (limit < 0)
{
return Unlimited;
}
highest = Math.Max(highest, limit);
}
return highest;
}This version returns -1 for both (-1, 500) and (500, -1), and it still returns 500 for (100, 500). Additionally, an empty list returns 0 here instead of throwing an exception like Max does, so please check that this is the right meaning for your own system.
The node already has a parent
The second bug depends on a small difference between two classes in System.Text.Json. We had a JSON redactor that reassigned the children of each node while it was walking them, including the children that had not changed. The code below is a simplified illustration of this logic and not the original code:
case JsonObject obj:
foreach (var name in obj.Select(p => p.Key).ToList())
{
obj[name] = SensitiveNames.Contains(name)
? JsonValue.Create("[redacted]")
: Redact(obj[name]);
}
return obj;
case JsonArray array:
for (var i = 0; i < array.Count; i++)
{
array[i] = Redact(array[i]);
}
return array;For objects this code works without any problem, but an array with a value in it throws this exception:
InvalidOperationException: The node already has a parent.Now let's examine why the two cases are different. In the .NET 10 source, JsonObject.SetItem has a ReferenceEquals check and returns early if you assign the same node back to the same property. On the other hand, JsonArray.SetItem has no such check and calls AssignParent first. That call throws because the node already has a parent, which is the same array! This code also passes with an empty array, so a test that sends only objects or empty arrays cannot find the bug.
In our case, any batch with an event containing an array of strings or objects was refused with a 500, including the other events in that batch, and the tracking of one application failed in this way for about 5 days. The fix was to assign back only when the child was actually replaced, plus a regression test that sends arrays:
case JsonObject obj:
foreach (var name in obj.Select(p => p.Key).ToList())
{
var child = obj[name];
var result = SensitiveNames.Contains(name)
? JsonValue.Create("[redacted]")
: Redact(child);
if (!ReferenceEquals(child, result))
{
obj[name] = result;
}
}
return obj;
case JsonArray array:
for (var i = 0; i < array.Count; i++)
{
var child = array[i];
var result = Redact(child);
if (!ReferenceEquals(child, result))
{
array[i] = result;
}
}
return array;With this version, arrays of values, objects inside arrays, nested arrays and null elements all pass, and a sensitive value inside an array is still replaced. Behind a real HTTP endpoint, the old version returns 500 for {"a":[1]} and the new one returns 200.
A service parameter that became a request body
Minimal APIs infer where a parameter comes from, and a registered service does not need [FromServices], which is very convenient. However, this inference depends on what is registered in that specific host. In one of my projects, a parameter without the attribute was inferred as a request body on test hosts that don't register the service. This failed the test job of the deploy, and two merged PRs sat undeployed until a one-line fix.
To see the behaviour, consider this example endpoint:
app.MapPost("/events", (TrackingEvent evt, IEventSink sink) =>
{
sink.Write(evt);
return Results.Accepted();
});If IEventSink is registered, this endpoint returns 202. If it is not registered, the framework takes evt as the inferred body and has no source for sink, so building the endpoints fails with this message:
InvalidOperationException: Failure to infer one or more parameters.The effect goes further than this one endpoint, because the host starts normally and the failure comes with the requests. Specifically, POST /events returned 500 and a separate GET /health on the same host also returned 500. A GET endpoint with the same mistake does the same with a different message, since GET does not bind from the body implicitly:
InvalidOperationException: Body was inferred but the method does not allow inferred body parameters.On the other hand, if the service is the only parameter of a POST, it really is inferred as the body. In that case GET /health stays at 200, and only the endpoint fails at request time, because System.Text.Json cannot deserialize an interface.
The fix is one attribute, which removes the guess:
app.MapPost("/events", (TrackingEvent evt, [FromServices] IEventSink sink) =>
{
sink.Write(evt);
return Results.Accepted();
});With this version and without the service, GET /health returns 200. The missing service is still missing, of course, so POST /events returns 500 with No service for type in the exception message, but now only the endpoint that really needs the service is affected.
Uri.ToString() and a SAS link
A SAS link is a signed URL, so it is better to pass it along without rewriting it. In one of my projects we build such a URL with AbsolutePath + Query, as a precaution and not because of a bug that we hit, since ToString() is documented as the display form of a URI and we needed the form that goes on the wire. The first line below is an example link with a made-up signature, and the second line is what ToString() returns for it:
https://account.blob.core.windows.net/reports/q3%20summary.pdf?sv=2025-07-05&sr=b&sp=r&rscd=attachment%3B%20filename%3D%22q3%20summary.pdf%22&sig=ab%2Bcd%2Fef%3D
https://account.blob.core.windows.net/reports/q3 summary.pdf?sv=2025-07-05&sr=b&sp=r&rscd=attachment%3B filename%3D"q3 summary.pdf"&sig=ab%2Bcd%2Fef%3Dvar uri = new Uri(link);
var unescaped = uri.ToString(); // a raw space in the path and the query
var preserved = uri.AbsolutePath + uri.Query; // the escapes are kept
var verbatim = uri.OriginalString; // exactly the string you passed inThe Microsoft documentation says that all characters are unescaped except #, ? and %, but on .NET 10 the behaviour is more selective. The %20 and %22 were decoded, while %2B, %2F, %3B and %3D stayed escaped, so the signature in this example survived and the change is in the path and the file name. The result has raw spaces and quotes, and RFC 3986 does not allow those in a URI. Passing that string to new Uri again gives back the original AbsoluteUri, so the problem appears when the string is used as plain text. This example was not tested against real Azure Storage, so it says nothing about how the service treats each form.
Additionally, AbsolutePath + Query and AbsoluteUri kept the example above unchanged, but this is not a byte-for-byte guarantee for every link. That's because the Uri class normalises escapes of unreserved characters, so %7E became ~ and %41 became A. If you really need the exact string that you received, only OriginalString gives you that.
Two names that the tools rejected
The last two are very small, and both of them are only about a name. In one project, the analyzer rule CA1711 rejected a class named SpeakerQueue:
public class SpeakerQueue
{
private readonly List<string> _speakers = [];
public void Add(string speaker) => _speakers.Add(speaker);
}warning CA1711: Rename type name SpeakerQueue so that it does not end in 'Queue'The reason is that Queue is one of the reserved suffixes of this rule, together with Collection, Dictionary, Stream and others. According to the documentation the rule is not enabled by default in .NET 10, so the default build passes, AnalysisMode=All gives the warning, and adding TreatWarningsAsErrors makes it an error. Additionally, the rule looks only at externally visible identifiers by default, so an internal class passes. A class that derives from System.Collections.Queue also passes, and so does one that derives from Queue<string>, although the documentation lists only the non-generic type. The documented fix is to remove the suffix, and if the word has an unambiguous meaning in your domain, you can allow it in .editorconfig:
[*.cs]
dotnet_code_quality.ca1711.allowed_suffixes = QueueThe second name is a controller action called Request, which clashes with the ControllerBase.Request property:
[ApiController]
public class SpeakersController : ControllerBase
{
[HttpPost("speakers/request")]
public IActionResult Request() => Ok(Request.Headers.UserAgent.ToString());
}The declaration produces warning CS0108, because the method hides the inherited member, and the use of Request.Headers produces an error, because the name now means the method:
warning CS0108: 'SpeakersController.Request()' hides inherited member 'ControllerBase.Request'. Use the new keyword if hiding was intended.
error CS0119: 'SpeakersController.Request()' is a method, which is not valid in the given contextAn action that never uses the property builds with only the warning, but the property stays hidden for anyone who edits that class later. Renaming the method solves it, and the URL stays the same because it comes from the route attribute:
[ApiController]
public class SpeakersController : ControllerBase
{
[HttpPost("speakers/request")]
public IActionResult RequestSpeaker() => Ok(Request.Headers.UserAgent.ToString());
}What these bugs have in common
None of these five needs an advanced fix, and each one depends on something that the code does not show directly. That can be the meaning of -1, a check that exists in one class and is missing in its sibling, the services of a specific host, or a suffix that a rule has reserved. For this reason, a test with the real input shape helps very much, such as an unlimited value in the list, an array in the JSON, a host with the same registrations as the failing one, or a link with a space in it.
References
- JsonArray.cs in dotnet/runtime, release/10.0
- JsonObject.cs in dotnet/runtime, release/10.0
- Parameter binding in Minimal API apps, ASP.NET Core 10.0
- Uri.ToString, .NET 10
- CA1711: Identifiers should not have incorrect suffix
- ControllerBase.Request, ASP.NET Core 10.0
- Compiler warning CS0108
- RFC 3986, Uniform Resource Identifier: Generic Syntax
Keep reading

November 3, 2026 · 8 min
10 November: What Actually Breaks When .NET 8 and In-Process Azure Functions Run Out of Support
.NET 8, .NET 9 and the Azure Functions in-process model all reach end of support on 10 November 2026. What is confirmed, what happens to running apps, how to find what you run, and the JSON attribute trap that fails without any error.
Read
October 29, 2026 · 8 min
Idempotent Bulk Ingestion into NebulaGraph from .NET: Solving the Duplicate Problem at the VID Level
Duplicates in a graph database are a key-design problem, not a row-uniqueness problem. This article shows how to engineer idempotency into NebulaGraph imports from .NET by controlling VIDs, edge ranks, and batch checkpointing.
Read
October 29, 2026 · 7 min
Graphs as Agent Memory: Why Vectors Alone Are Not Enough
Vector search tells your agent what looks similar; it cannot tell it what depends on what. This article shows concretely where embedding retrieval fails for dependency reasoning, then models a real service graph in NebulaGraph 3.7 with full C# wiring and traversal code.
Read