{"record":{"id":"d72a29b97b293a11","repo":"louthy/language-ext","slug":"indexoutofrangeexception-seqemptyinternal","errorCode":null,"errorMessage":"IndexOutOfRangeException","messagePattern":"IndexOutOfRangeException","errorType":"exception","errorClass":"IndexOutOfRangeException","httpStatus":null,"severity":"error","filePath":"LanguageExt.Core/Immutable Collections/Seq/DSL/SeqEmptyInternal.cs","lineNumber":18,"sourceCode":"﻿using System;\nusing System.Collections;\nusing System.Collections.Generic;\nusing System.Runtime.CompilerServices;\nusing LanguageExt.Common;\n\nnamespace LanguageExt;\n\ninternal class SeqEmptyInternal<A> : ISeqInternal<A>\n{\n    public static ISeqInternal<A> Default = new SeqEmptyInternal<A>();\n\n    [MethodImpl(MethodImplOptions.AggressiveInlining)]\n    public ReadOnlySpan<A> AsSpan() =>\n        ReadOnlySpan<A>.Empty;\n\n    public A this[int index] => \n        throw new IndexOutOfRangeException();\n\n    public Option<A> At(int index) => \n        default;\n\n    public A Head =>\n        throw Exceptions.SequenceEmpty;\n\n    public ISeqInternal<A> Tail =>\n        this;\n\n    public bool IsEmpty => \n        true;\n\n    public ISeqInternal<A> Init =>\n        this;\n\n    public A Last =>\n        throw Exceptions.SequenceEmpty;","sourceCodeStart":1,"sourceCodeEnd":36,"githubUrl":"https://github.com/louthy/language-ext/blob/2f0e3628242889774d4141960a35671a0280051f/LanguageExt.Core/Immutable Collections/Seq/DSL/SeqEmptyInternal.cs#L1-L36","documentation":"SeqEmptyInternal is the empty sequence sentinel; its indexer always throws IndexOutOfRangeException because an empty sequence has no elements. Any code path that indexes an empty Seq without checking lands here.","triggerScenarios":"Indexing an empty Seq via seq[i] (the empty-seq representation is SeqEmptyInternal); accessing seq[0] after filtering/Where that removed all elements.","commonSituations":"Filtering a collection and assuming at least one result remains; indexing Head-less results of Empty, Filter, or Take(0); off-by-one on zero-length input.","solutions":["Check seq.IsEmpty / seq.Count > 0 before indexing","Use HeadOrNone / At which return Option instead of throwing","Pattern-match: seq.Match(x => x[0], () => fallback)"],"exampleFix":"// before\nvar first = seq[0];\n// after\nvar first = seq.HeadOrNone().IfNone(default(A));","handlingStrategy":"type-guard","validationCode":"bool hasFirst = seq.Count > 0;","typeGuard":"Option<A> TryFirst<A>(Seq<A> seq) => seq.HeadOrNone();","tryCatchPattern":"try { x = seq[index]; }\ncatch (IndexOutOfRangeException)\n{ x = fallback; }","preventionTips":["Always check IsEmpty after Filter/Where before indexing","Prefer HeadOrNone/At Option-based accessors","Use Match over the seq instead of manual index access"],"tags":["csharp","languageext","seq","empty-sequence","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"}