{"record":{"id":"7b74c914a5f45b33","repo":"louthy/language-ext","slug":"indexoutofrangeexception-seqlazy","errorCode":null,"errorMessage":"IndexOutOfRangeException","messagePattern":"IndexOutOfRangeException","errorType":"exception","errorClass":"IndexOutOfRangeException","httpStatus":null,"severity":"error","filePath":"LanguageExt.Core/Immutable Collections/Seq/DSL/SeqLazy.cs","lineNumber":83,"sourceCode":"    [MethodImpl(MethodImplOptions.AggressiveInlining)]\n    SeqLazy(A[] data, int start, int count, int noCons, Enum<A> seq, int seqStart)\n    {\n        this.data = data;\n        this.start = start;\n        this.count = count;\n        this.seq = seq;\n        this.seqStart = seqStart;\n        consDisallowed = noCons;\n    }\n\n    public A this[int index]\n    {\n        [MethodImpl(MethodImplOptions.AggressiveInlining)]\n        get\n        {\n            var r = At(index);\n            if (r.IsSome) return r.Value!;\n            throw new IndexOutOfRangeException();\n        }\n    }\n\n    [MethodImpl(MethodImplOptions.AggressiveInlining)]\n    public Option<A> At(int index)\n    {\n        if (index < 0) return default;\n        if (index < count) return data[^count];\n        var lazyIndex = index                      - count + seqStart;\n        var (succ, val) = StreamTo(lazyIndex);\n        return succ\n                   ? val\n                   : default(Option<A>);\n    }\n\n    [MethodImpl(MethodImplOptions.AggressiveInlining)]\n    (bool Success, A? Value) StreamTo(int index)\n    {","sourceCodeStart":65,"sourceCodeEnd":101,"githubUrl":"https://github.com/louthy/language-ext/blob/2f0e3628242889774d4141960a35671a0280051f/LanguageExt.Core/Immutable Collections/Seq/DSL/SeqLazy.cs#L65-L101","documentation":"SeqLazy's indexer evaluates At(index), which forces the underlying lazy enumerator up to the index; if the option comes back None (index beyond available elements) it throws IndexOutOfRangeException. Note At on a lazy seq consumes elements up to the index.","triggerScenarios":"Indexing a lazily-evaluated Seq with an index >= the number of elements the lazy source yields, or < 0.","commonSituations":"Indexing results of infinite/lazy generators by a fixed index assuming a minimum length; iterating with stale Count after the source changed; off-by-one with <= bounds.","solutions":["Check bounds first; for lazy seqs prefer Take + iteration over random access","Use At()/TryAt-style Option access instead of the indexer when length is unknown","Materialize with ToSeq()/ToArray() if you need reliable indexing and length"],"exampleFix":"// before\nvar x = lazySeq[10];\n// after\nvar x = lazySeq.At(10).IfNone(default(A));","handlingStrategy":"type-guard","validationCode":"bool inBounds = index >= 0 && index < lazySeq.Count; // forces evaluation","typeGuard":"Option<A> TryAt<A>(SeqLazy<A> seq, int i) => seq.At(i);","tryCatchPattern":"try { x = lazySeq[index]; }\ncatch (IndexOutOfRangeException)\n{ x = fallback; }","preventionTips":["Remember lazy seqs may be shorter than assumed and At consumes elements","Materialize with ToSeq()/ToArray() before random access","Use At() returning Option when length is unknown"],"tags":["csharp","languageext","seq","lazy","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"}