{"record":{"id":"09b68479491ca7f2","repo":"louthy/language-ext","slug":"indexoutofrangeexception-lst-internal","errorCode":null,"errorMessage":"IndexOutOfRangeException","messagePattern":"IndexOutOfRangeException","errorType":"exception","errorClass":"IndexOutOfRangeException","httpStatus":null,"severity":"error","filePath":"LanguageExt.Core/Immutable Collections/List/Internal/Lst.Internal.cs","lineNumber":94,"sourceCode":"        this.root = root;\n    }\n\n    internal ListItem<A> Root\n    {\n        [MethodImpl(MethodImplOptions.AggressiveInlining)]\n        get => root;\n    }\n\n    /// <summary>\n    /// Index accessor\n    /// </summary>\n    [Pure]\n    public A this[int index]\n    {\n        [MethodImpl(MethodImplOptions.AggressiveInlining)]\n        get\n        {\n            if (index < 0 || index >= Root.Count) throw new IndexOutOfRangeException();\n            return ListModule.GetItem(Root, index);\n        }\n    }\n\n    /// <summary>\n    /// Number of items in the list\n    /// </summary>\n    [Pure]\n    public int Count\n    {\n        [MethodImpl(MethodImplOptions.AggressiveInlining)]\n        get => Root.Count;\n    }\n\n    [Pure]\n    int IReadOnlyCollection<A>.Count\n    {\n        [MethodImpl(MethodImplOptions.AggressiveInlining)]","sourceCodeStart":76,"sourceCodeEnd":112,"githubUrl":"https://github.com/louthy/language-ext/blob/2f0e3628242889774d4141960a35671a0280051f/LanguageExt.Core/Immutable Collections/List/Internal/Lst.Internal.cs#L76-L112","documentation":"This IndexOutOfRangeException is a bounds guard in the Lst<A> indexer: it throws whenever the caller indexes outside the valid range [0, Root.Count). The faulting input is the index argument passed to the indexer; it indicates off-by-one logic or an assumption the list is longer than it is.","triggerScenarios":"`lst[i]` with i == lst.Count or i < 0; indexing after removals using a stale index; iterating with `for (i...) lst[i]` where the bound uses a different (larger) collection's count.","commonSituations":"Off-by-one loop conditions (`<=` instead of `<`); holding an index across a mutation; parsing loops that increment past the end.","solutions":["Validate: `if (i >= 0 && i < lst.Count)` before indexing.","Prefer foreach/Fold/Map iteration over index-based access.","In generic code, guard the index or clamp it with Math.Clamp/Math.Min before access."],"exampleFix":"// before\nvar x = lst[i];\n// after\nif (i < 0 || i >= lst.Count) throw new ArgumentOutOfRangeException(nameof(i));\nvar x = lst[i];","handlingStrategy":"validation","validationCode":"if (index < 0 || index >= lst.Count) throw new ArgumentOutOfRangeException(nameof(index));\nvar x = lst[index];","typeGuard":"static Option<A> TryGet<A>(Lst<A> lst, int i) => (uint)i < (uint)lst.Count ? Prelude.Some(lst[i]) : Prelude.None;","tryCatchPattern":"try { x = lst[index]; }\ncatch (IndexOutOfRangeException) { x = default; /* or rethrow as domain error */ }","preventionTips":["Recheck Count immediately before indexing; Lst is immutable but indices can be computed from other collections.","Use `(uint)i < (uint)count` idiom for fast, negative-safe bounds checks.","Prefer foreach/Fold over indexed loops.","Never cache indices across mutations (RemoveAt/Insert change positions)."],"tags":["csharp","languageext","list","indexing"],"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"}