{"record":{"id":"e96ddae544629bf1","repo":"louthy/language-ext","slug":"indexoutofrangeexception-set-internal","errorCode":null,"errorMessage":"IndexOutOfRangeException","messagePattern":"IndexOutOfRangeException","errorType":"exception","errorClass":"IndexOutOfRangeException","httpStatus":null,"severity":"error","filePath":"LanguageExt.Core/Immutable Collections/Set/Internal/Set.Internal.cs","lineNumber":660,"sourceCode":"        foreach (A item in other)\n        {\n            if (Contains(item))\n            {\n                return true;\n            }\n        }\n        return false;\n    }\n\n    /// <summary>\n    /// Copy the items from the set into the specified array\n    /// </summary>\n    /// <param name=\"array\">Array to copy to</param>\n    /// <param name=\"index\">Index into the array to start</param>\n    public void CopyTo(A[] array, int index)\n    {\n        if (array == null) throw new ArgumentNullException(nameof(array));\n        if (index < 0 || index > array.Length) throw new IndexOutOfRangeException();\n        if (index + Count > array.Length) throw new IndexOutOfRangeException();\n\n        foreach (var element in this)\n        {\n            array[index++] = element;\n        }\n    }\n\n    /// <summary>\n    /// Copy the items from the set into the specified array\n    /// </summary>\n    /// <param name=\"array\">Array to copy to</param>\n    /// <param name=\"index\">Index into the array to start</param>\n    public void CopyTo(Array array, int index)\n    {\n        if (array == null) throw new ArgumentNullException(nameof(array));\n        if (index < 0 || index > array.Length) throw new IndexOutOfRangeException();\n        if (index + Count > array.Length) throw new IndexOutOfRangeException();","sourceCodeStart":642,"sourceCodeEnd":678,"githubUrl":"https://github.com/louthy/language-ext/blob/2f0e3628242889774d4141960a35671a0280051f/LanguageExt.Core/Immutable Collections/Set/Internal/Set.Internal.cs#L642-L678","documentation":"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.","triggerScenarios":"Calling set.CopyTo(arr, idx) with idx < 0, idx > arr.Length, or arr.Length - idx < set.Count.","commonSituations":"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.","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"],"exampleFix":"// before\nvar buf = new A[10];\nset.CopyTo(buf, 5); // needs Count <= 5\n// after\nvar buf = new A[set.Count];\nset.CopyTo(buf, 0);","handlingStrategy":"validation","validationCode":"if (array == null || index < 0 || index > array.Length || array.Length - index < set.Count) return false;\nset.CopyTo(array, index);","typeGuard":"static bool FitsInArray<A>(Set<A> set, A[] arr, int idx) => arr != null && idx >= 0 && idx <= arr.Length && arr.Length - idx >= set.Count;","tryCatchPattern":"try { set.CopyTo(arr, idx); }\ncatch (IndexOutOfRangeException) { arr = new A[set.Count]; set.CopyTo(arr, 0); }","preventionTips":["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"],"tags":["csharp","languageext","set","copyto","bounds"],"backgroundTag":"argument-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"}