louthy/language-ext · error · ArgumentOutOfRangeException

ArgumentOutOfRangeException

Error message

ArgumentOutOfRangeException

What it means

LstInternal.FindRange(index, count) throws ArgumentOutOfRangeException when index < 0 or index >= Count — the slice start must point at an existing element. Unlike RemoveRange this uses ArgumentOutOfRangeException with the parameter name, reflecting its read-only slicing nature.

Solutions

  1. Check 0 <= index < list.Count before calling FindRange.
  2. Handle empty lists before slicing (Count == 0 means any index is invalid).
  3. Compute page starts as Math.Min(page*pageSize, Count-1) and skip when the list is empty.
  4. Prefer Skip(index).Take(count)-style APIs if you want empty results instead of exceptions.

Example fix

// before
var page = lst.FindRange(pageIndex * pageSize, pageSize);
// after
var start = pageIndex * pageSize;
var page = lst.Count == 0 || start >= lst.Count
    ? Iterable.empty<A>()
    : lst.FindRange(start, pageSize);
Defensive patterns

Strategy: validation

Validate before calling

var page = (lst.Count > 0 && start >= 0 && start < lst.Count)
    ? lst.FindRange(start, count)
    : Iterable.empty<A>();

Type guard

static bool CanFindRange<A>(Lst<A> lst, int index, int count) =>
    lst.Count > 0 && index >= 0 && index < lst.Count && count >= 0;

Try / catch

try { var page = lst.FindRange(start, size); }
catch (ArgumentOutOfRangeException)
{
    var page = Iterable.empty<A>(); // past-the-end page
}

Prevention

When it happens

Trigger: FindRange(-1, n); FindRange(list.Count, n) (e.g. paging past the end); FindRange on an empty list where Count == 0 so even index 0 is out of range.

Common situations: Paging logic where page * pageSize lands exactly on or past Count; slicing an empty result set from a query; start index computed from a larger prior snapshot.

Related errors


AI-assisted analysis of louthy/language-ext@2f0e362824 (2026-09-15). Data as JSON: /api/errors/574552d760583077. Report an issue: GitHub.

Appendix: source

Thrown at LanguageExt.Core/Immutable Collections/List/Internal/Lst.Internal.cs:346

        {
            state = folder(state, item);
        }
        return state;
    }

    /// <summary>
    /// Map
    /// </summary>
    [Pure]
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public LstInternal<U> Map<U>(Func<A, U> map) =>
        new (this.AsEnumerable().Select(map));

    [Pure]
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public Iterable<A> FindRange(int index, int count)
    {
        if (index < 0 || index >= Count) throw new ArgumentOutOfRangeException(nameof(index));
        if (count < 0) throw new ArgumentOutOfRangeException(nameof(index));
        return Iterable.createRange(Go());

        IEnumerable<A> Go()
        {
            var iter = new ListEnumerator<A>(Root, false, index, count);
            while (iter.MoveNext())
            {
                yield return iter.Current;
            }
        }
    }

    /// <summary>
    /// Filter
    /// </summary>
    [Pure]
    [MethodImpl(MethodImplOptions.AggressiveInlining)]

View on GitHub (pinned to 2f0e362824)