AvaloniaUI/Avalonia · error · ArgumentOutOfRangeException
Specified argument was out of the range of valid values. (Pa
Error message
Specified argument was out of the range of valid values. (Parameter 'capacity')
What it means
CompositeDisposable(int capacity) pre-allocates its internal List<IDisposable?> with the given capacity and throws ArgumentOutOfRangeException(nameof(capacity)) when capacity < 0, because List<T> itself rejects negative capacity. This mirrors the BCL collection contract for capacity parameters.
Source
Thrown at src/Avalonia.Base/Reactive/CompositeDisposable.cs:25
internal sealed class CompositeDisposable : ICollection<IDisposable>, IDisposable
{
private readonly object _gate = new object();
private bool _disposed;
private List<IDisposable?> _disposables;
private int _count;
private const int ShrinkThreshold = 64;
/// <summary>
/// Initializes a new instance of the <see cref="CompositeDisposable"/> class with the specified number of disposables.
/// </summary>
/// <param name="capacity">The number of disposables that the new CompositeDisposable can initially store.</param>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="capacity"/> is less than zero.</exception>
public CompositeDisposable(int capacity)
{
if (capacity < 0)
{
throw new ArgumentOutOfRangeException(nameof(capacity));
}
_disposables = new List<IDisposable?>(capacity);
}
/// <summary>
/// Initializes a new instance of the <see cref="CompositeDisposable"/> class from a group of disposables.
/// </summary>
/// <param name="disposables">Disposables that will be disposed together.</param>
/// <exception cref="ArgumentNullException"><paramref name="disposables"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException">Any of the disposables in the <paramref name="disposables"/> collection is <c>null</c>.</exception>
public CompositeDisposable(params IDisposable[] disposables)
{
if (disposables == null)
{
throw new ArgumentNullException(nameof(disposables));
}
View on GitHub (pinned to 11c5427268)
Solutions
- Clamp the capacity to Math.Max(0, value) before constructing.
- Validate the source count/size and default to 0 when it would be negative.
- Prefer the parameterless or params-array constructors when exact capacity is uncertain.
Example fix
// before var cd = new CompositeDisposable(items.Count - reserved); // after var cd = new CompositeDisposable(Math.Max(0, items.Count - reserved));
Defensive patterns
Strategy: validation
Validate before calling
int cap = Math.Max(0, computedCapacity); var cd = new CompositeDisposable(cap);
Type guard
bool ValidCapacity(int c) => c >= 0;
Prevention
- Clamp computed capacity to >= 0.
- Default uncertain capacities to 0 (grows as needed).
- Unit-test capacity derived from empty/underflowing inputs.
When it happens
Trigger: Constructing new CompositeDisposable(negativeNumber) where the capacity value came from a computed/size expression that underflowed to negative.
Common situations: Capacity computed as (count - someOffset) that goes negative on empty input, or a misconfigured size constant.
Related errors
- Specified argument was out of the range of valid values. (Pa
- Value cannot be null. (Parameter 'disposables')
- Disposables can't contain null
- Value cannot be null. (Parameter 'item')
- Value cannot be null. (Parameter 'array')
AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13).
Data as JSON: /api/errors/9ebd84909f90ccb9.
Report an issue: GitHub.