louthy/language-ext · error · IndexOutOfRangeException
IndexOutOfRangeException
Error message
IndexOutOfRangeException
What it means
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.
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)
Example fix
// before var first = seq[0]; // after var first = seq.HeadOrNone().IfNone(default(A));
Defensive patterns
Strategy: type-guard
Validate before calling
bool hasFirst = seq.Count > 0;
Type guard
Option<A> TryFirst<A>(Seq<A> seq) => seq.HeadOrNone();
Try / catch
try { x = seq[index]; }
catch (IndexOutOfRangeException)
{ x = fallback; } Prevention
- Always check IsEmpty after Filter/Where before indexing
- Prefer HeadOrNone/At Option-based accessors
- Use Match over the seq instead of manual index access
When it happens
Trigger: Indexing an empty Seq via seq[i] (the empty-seq representation is SeqEmptyInternal); accessing seq[0] after filtering/Where that removed all elements.
Common situations: 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.
Related errors
- IndexOutOfRangeException
- IndexOutOfRangeException
- IndexOutOfRangeException
- Nil iterator has no head
- Nil iterator has no head
AI-assisted analysis of louthy/language-ext@2f0e362824 (2026-09-15).
Data as JSON: /api/errors/d72a29b97b293a11.
Report an issue: GitHub.
Appendix: source
Thrown at LanguageExt.Core/Immutable Collections/Seq/DSL/SeqEmptyInternal.cs:18
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using LanguageExt.Common;
namespace LanguageExt;
internal class SeqEmptyInternal<A> : ISeqInternal<A>
{
public static ISeqInternal<A> Default = new SeqEmptyInternal<A>();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ReadOnlySpan<A> AsSpan() =>
ReadOnlySpan<A>.Empty;
public A this[int index] =>
throw new IndexOutOfRangeException();
public Option<A> At(int index) =>
default;
public A Head =>
throw Exceptions.SequenceEmpty;
public ISeqInternal<A> Tail =>
this;
public bool IsEmpty =>
true;
public ISeqInternal<A> Init =>
this;
public A Last =>
throw Exceptions.SequenceEmpty;View on GitHub (pinned to 2f0e362824)