louthy/language-ext · error · IndexOutOfRangeException
IndexOutOfRangeException
Error message
IndexOutOfRangeException
What it means
Set.CopyTo throws IndexOutOfRangeException when the index is negative, greater than the array length, or when index + Count exceeds the array capacity. It is the bounds check ensuring the set's elements fit into the target array starting at the given offset.
Solutions
- Size the array as index + set.Count (or just set.Count when starting at 0) before copying
- Copy from index 0 into a fresh arr = new A[set.Count]
- Use ToArray()/ToArray extension to avoid manual bounds math
- Clamp/validate caller-supplied index against array bounds before invoking
Example fix
// before var buf = new A[10]; set.CopyTo(buf, 5); // needs Count <= 5 // after var buf = new A[set.Count]; set.CopyTo(buf, 0);
Defensive patterns
Strategy: validation
Validate before calling
if (array == null || index < 0 || index > array.Length || array.Length - index < set.Count) return false; set.CopyTo(array, index);
Type guard
static bool FitsInArray<A>(Set<A> set, A[] arr, int idx) => arr != null && idx >= 0 && idx <= arr.Length && arr.Length - idx >= set.Count;
Try / catch
try { set.CopyTo(arr, idx); }
catch (IndexOutOfRangeException) { arr = new A[set.Count]; set.CopyTo(arr, 0); } Prevention
- Size the destination as index + set.Count before copying
- Re-size buffers if the set may have grown since sizing
- Use ToArray() to sidestep manual bounds arithmetic
When it happens
Trigger: Calling set.CopyTo(arr, idx) with idx < 0, idx > arr.Length, or arr.Length - idx < set.Count.
Common situations: Reusing a small scratch buffer for a larger set; offset arithmetic errors (page/segment-based copies); forgetting the set can grow between sizing the array and copying.
Related errors
- ArgumentNullException
- Ord attribute should have a struct type that derives from…
- Hashable attribute should have a struct type that derives…
- Don't use Equals - use either RecordType
- Don't use Equals - use either RecordType
AI-assisted analysis of louthy/language-ext@2f0e362824 (2026-09-15).
Data as JSON: /api/errors/e96ddae544629bf1.
Report an issue: GitHub.
Appendix: source
Thrown at LanguageExt.Core/Immutable Collections/Set/Internal/Set.Internal.cs:660
foreach (A item in other)
{
if (Contains(item))
{
return true;
}
}
return false;
}
/// <summary>
/// Copy the items from the set into the specified array
/// </summary>
/// <param name="array">Array to copy to</param>
/// <param name="index">Index into the array to start</param>
public void CopyTo(A[] array, int index)
{
if (array == null) throw new ArgumentNullException(nameof(array));
if (index < 0 || index > array.Length) throw new IndexOutOfRangeException();
if (index + Count > array.Length) throw new IndexOutOfRangeException();
foreach (var element in this)
{
array[index++] = element;
}
}
/// <summary>
/// Copy the items from the set into the specified array
/// </summary>
/// <param name="array">Array to copy to</param>
/// <param name="index">Index into the array to start</param>
public void CopyTo(Array array, int index)
{
if (array == null) throw new ArgumentNullException(nameof(array));
if (index < 0 || index > array.Length) throw new IndexOutOfRangeException();
if (index + Count > array.Length) throw new IndexOutOfRangeException();View on GitHub (pinned to 2f0e362824)