{"record":{"id":"f0c5e30535e5699c","repo":"louthy/language-ext","slug":"indexoutofrangeexception","errorCode":null,"errorMessage":"IndexOutOfRangeException","messagePattern":"IndexOutOfRangeException","errorType":"exception","errorClass":"IndexOutOfRangeException","httpStatus":null,"severity":"error","filePath":"LanguageExt.Core/Immutable Collections/Arr/Arr.cs","lineNumber":126,"sourceCode":"    /// <returns>A read-only span of values</returns>\n    /// <exception cref=\"IndexOutOfRangeException\">Thrown If the start index is outside the range of the array</exception>\n    [Pure]\n    [MethodImpl(MethodImplOptions.AggressiveInlining)]\n    public ReadOnlySpan<A> AsSpan() =>\n        new (Value, start, length);\n\n    /// <summary>\n    /// Create a readonly sub-span of this array.  This doesn't do any copying, so is very fast, but be aware that any\n    /// items outside the splice are still active.   \n    /// </summary>\n    /// <param name=\"start\">Offset from the beginning of the array</param>\n    /// <returns>A read-only span of values</returns>\n    /// <exception cref=\"IndexOutOfRangeException\">Thrown If the start index is outside the range of the array</exception>\n    [Pure]\n    [MethodImpl(MethodImplOptions.AggressiveInlining)]\n    public ReadOnlySpan<A> AsSpan(int start)\n    {\n        if (start < 0 || start >= length) throw new IndexOutOfRangeException(nameof(start));\n        var t = Math.Max(0, length - start);\n        return new(Value, this.start + start, t);\n    }\n\n    /// <summary>\n    /// Create a readonly sub-span of this array.  This doesn't do any copying, so is very fast, but be aware that any\n    /// items outside the splice are still active.   \n    /// </summary>\n    /// <param name=\"start\">Offset from the beginning of the array</param>\n    /// <param name=\"count\">The number of items to take. This will be clamped to the maximum number of items available</param>\n    /// <returns>A read-only span of values</returns>\n    /// <exception cref=\"IndexOutOfRangeException\">Thrown If the start index is outside the range of the array</exception>\n    [Pure]\n    [MethodImpl(MethodImplOptions.AggressiveInlining)]\n    public ReadOnlySpan<A> AsSpan(int start, int count)\n    {\n        if (start < 0 || start >= length) throw new IndexOutOfRangeException(nameof(start));\n        var t = Math.Max(0, Math.Min(count, length - start));","sourceCodeStart":108,"sourceCodeEnd":144,"githubUrl":"https://github.com/louthy/language-ext/blob/2f0e3628242889774d4141960a35671a0280051f/LanguageExt.Core/Immutable Collections/Arr/Arr.cs#L108-L144","documentation":"AsSpan(int start) returns a read-only span beginning at the given index of the Arr. It throws IndexOutOfRangeException when start is negative or >= the Arr's length. The XML doc on the method explicitly declares this exception for an out-of-range start index.","triggerScenarios":"Calling arr.AsSpan(start) where start < 0 or start >= arr.Length, most commonly start == arr.Length on an empty or exactly-exhausted Arr.","commonSituations":"Iterating windows in a loop without re-checking the shrinking length, hot-path parsing code that assumes a minimum span size, or slicing an Arr that unexpectedly came back empty from a filter/map chain.","solutions":["Check start is within [0, arr.Length) before calling AsSpan; use arr.AsSpan() for the whole span.","Clamp the index: arr.AsSpan(Math.Clamp(start, 0, arr.Length - 1)) when a valid non-empty Arr is guaranteed.","Handle empty Arrs by returning ReadOnlySpan<A>.Empty instead of slicing."],"exampleFix":"// before\nvar span = arr.AsSpan(offset);\n// after\nvar span = offset >= 0 && offset < arr.Count ? arr.AsSpan(offset) : ReadOnlySpan<int>.Empty;","handlingStrategy":"validation","validationCode":"bool ok = start >= 0 && start < arr.Count;\nif (!ok) return ReadOnlySpan<int>.Empty;\nvar span = arr.AsSpan(start);","typeGuard":"static bool CanSpanAt<T>(Arr<T> a, int start) => (uint)start < (uint)a.Count;","tryCatchPattern":"try { return arr.AsSpan(start); }\ncatch (IndexOutOfRangeException) { return ReadOnlySpan<int>.Empty; }","preventionTips":["Re-check arr.Count inside loops that advance a start offset.","Use parameterless AsSpan() when you want the whole array.","Never assume a minimum length for an Arr derived from external data."],"tags":["languageext","arr","span","index-out-of-range"],"backgroundTag":"index-out-of-range","analyzedSha":"2f0e3628242889774d4141960a35671a0280051f","analyzedAt":"2026-09-15T03:31:55.716Z","contentChangedAt":"2026-09-15T03:31:55.716Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}