{"record":{"id":"9ebd84909f90ccb9","repo":"AvaloniaUI/Avalonia","slug":"specified-argument-was-out-of-the-range-of-valid-v","errorCode":null,"errorMessage":"Specified argument was out of the range of valid values. (Parameter 'capacity')","messagePattern":"Specified argument was out of the range of valid values\\. \\(Parameter 'capacity'\\)","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Reactive/CompositeDisposable.cs","lineNumber":25,"sourceCode":"\ninternal sealed class CompositeDisposable : ICollection<IDisposable>, IDisposable\n{\n    private readonly object _gate = new object();\n    private bool _disposed;\n    private List<IDisposable?> _disposables;\n    private int _count;\n    private const int ShrinkThreshold = 64;\n\n    /// <summary>\n    /// Initializes a new instance of the <see cref=\"CompositeDisposable\"/> class with the specified number of disposables.\n    /// </summary>\n    /// <param name=\"capacity\">The number of disposables that the new CompositeDisposable can initially store.</param>\n    /// <exception cref=\"ArgumentOutOfRangeException\"><paramref name=\"capacity\"/> is less than zero.</exception>\n    public CompositeDisposable(int capacity)\n    {\n        if (capacity < 0)\n        {\n            throw new ArgumentOutOfRangeException(nameof(capacity));\n        }\n\n        _disposables = new List<IDisposable?>(capacity);\n    }\n\n    /// <summary>\n    /// Initializes a new instance of the <see cref=\"CompositeDisposable\"/> class from a group of disposables.\n    /// </summary>\n    /// <param name=\"disposables\">Disposables that will be disposed together.</param>\n    /// <exception cref=\"ArgumentNullException\"><paramref name=\"disposables\"/> is <c>null</c>.</exception>\n    /// <exception cref=\"ArgumentException\">Any of the disposables in the <paramref name=\"disposables\"/> collection is <c>null</c>.</exception>\n    public CompositeDisposable(params IDisposable[] disposables)\n    {\n        if (disposables == null)\n        {\n            throw new ArgumentNullException(nameof(disposables));\n        }\n","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Reactive/CompositeDisposable.cs#L7-L43","documentation":"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.","triggerScenarios":"Constructing new CompositeDisposable(negativeNumber) where the capacity value came from a computed/size expression that underflowed to negative.","commonSituations":"Capacity computed as (count - someOffset) that goes negative on empty input, or a misconfigured size constant.","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."],"exampleFix":"// before\nvar cd = new CompositeDisposable(items.Count - reserved);\n\n// after\nvar cd = new CompositeDisposable(Math.Max(0, items.Count - reserved));","handlingStrategy":"validation","validationCode":"int cap = Math.Max(0, computedCapacity);\nvar cd = new CompositeDisposable(cap);","typeGuard":"bool ValidCapacity(int c) => c >= 0;","tryCatchPattern":null,"preventionTips":["Clamp computed capacity to >= 0.","Default uncertain capacities to 0 (grows as needed).","Unit-test capacity derived from empty/underflowing inputs."],"tags":["avalonia","reactive","disposables","argumentoutofrange","capacity"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}