{"record":{"id":"dc9ebde534cc3e1e","repo":"TheAlgorithms/C-Sharp","slug":"capacity-must-be-at-least-1","errorCode":null,"errorMessage":"Capacity must be at least 1.","messagePattern":"Capacity must be at least 1\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"DataStructures/Deque/Deque.cs","lineNumber":56,"sourceCode":"    ///     Initializes a new instance of the <see cref=\"Deque{T}\" /> class with default capacity.\n    ///     Default capacity is 16 elements, which provides a good balance between\n    ///     memory usage and avoiding early resizing for typical use cases.\n    /// </summary>\n    public Deque()\n        : this(16)\n    {\n    }\n\n    /// <summary>\n    ///     Initializes a new instance of the <see cref=\"Deque{T}\" /> class with specified capacity.\n    /// </summary>\n    /// <param name=\"capacity\">The initial capacity of the deque.</param>\n    /// <exception cref=\"ArgumentException\">Thrown when capacity is less than 1.</exception>\n    public Deque(int capacity)\n    {\n        if (capacity < 1)\n        {\n            throw new ArgumentException(\"Capacity must be at least 1.\", nameof(capacity));\n        }\n\n        items = new T[capacity];\n        front = 0;\n        rear = 0;\n        count = 0;\n    }\n\n    /// <summary>\n    ///     Gets the number of elements in the deque.\n    /// </summary>\n    public int Count => count;\n\n    /// <summary>\n    ///     Gets a value indicating whether the deque is empty.\n    /// </summary>\n    public bool IsEmpty => count == 0;\n","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/DataStructures/Deque/Deque.cs#L38-L74","documentation":"The Deque<T>(int capacity) constructor requires an initial capacity of at least 1. Passing 0 or a negative value cannot create the backing array, so an ArgumentException naming the 'capacity' parameter is thrown immediately at construction time.","triggerScenarios":"new Deque<int>(0) or new Deque<T>(-1) — any capacity argument less than 1, often from an unvalidated config value, a computed size of 0 for empty input, or a default(int) of 0.","commonSituations":"Passing a computed count that is 0 when the collection is empty; reading capacity from configuration where the key is missing/0; using default parameter values without validating them.","solutions":["Pass a positive capacity, e.g. new Deque<T>(Math.Max(1, requestedCapacity)).","If the initial size is unknown, use a small positive default (e.g. 4 or 16).","Validate the capacity at the call site before constructing and surface a clearer domain error.","If the capacity comes from config, clamp it: capacity = Math.Max(1, parsedCapacity)."],"exampleFix":"// before\nvar deque = new Deque<int>(items.Count); // throws when items is empty (0)\n\n// after\nvar deque = new Deque<int>(Math.Max(1, items.Count));","handlingStrategy":"validation","validationCode":"void EnsureCapacity(int c) { if (c < 1) throw new ArgumentOutOfRangeException(nameof(c), \"Capacity must be at least 1.\"); }","typeGuard":null,"tryCatchPattern":"try { var d = new Deque<T>(capacity); } catch (ArgumentException ex) when (ex.ParamName == \"capacity\") { capacity = 1; d = new Deque<T>(capacity); }","preventionTips":["Always clamp computed capacities: Math.Max(1, count).","Never pass default(int) (0) straight into the constructor.","Validate config-sourced capacity values at startup.","Default to a small positive capacity (e.g. 8) when size is unknown."],"tags":["csharp","data-structures","deque","argument-exception"],"backgroundTag":"invalid-constructor-argument","analyzedSha":"96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c","analyzedAt":"2026-09-13T17:04:01.438Z","contentChangedAt":"2026-09-13T17:04:01.438Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}