{"record":{"id":"16232daa45e3a754","repo":"dotnet/wpf","slug":"sr-arg-nonzerolowerbound","errorCode":null,"errorMessage":"SR.Arg_NonZeroLowerBound","messagePattern":"SR\\.Arg_NonZeroLowerBound","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Collections/ObjectModel/WeakReadOnlyCollection.cs","lineNumber":146,"sourceCode":"                    {\n                        System.Threading.Interlocked.CompareExchange<Object>(ref _syncRoot, new Object(), null);\n                    }\n                }\n                return _syncRoot;\n            }\n        }\n\n        void ICollection.CopyTo(Array array, int index) {\n            ArgumentNullException.ThrowIfNull(array);\n\n            if (array.Rank != 1) {\n                //ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_RankMultiDimNotSupported);\n                throw new ArgumentException(SR.Arg_RankMultiDimNotSupported);\n            }\n\n            if( array.GetLowerBound(0) != 0 ) {\n                //ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_NonZeroLowerBound);\n                throw new ArgumentException(SR.Arg_NonZeroLowerBound);\n            }\n\n            if (index < 0) {\n                //ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.arrayIndex, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);\n                throw new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_NeedNonNegNum);\n            }\n\n            if (array.Length - index < Count) {\n                //ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall);\n                throw new ArgumentException(SR.Arg_ArrayPlusOffTooSmall);\n            }\n\n            IList<T> dlist = CreateDereferencedList();\n            if (array is T[] items)\n            {\n                dlist.CopyTo(items, index);\n            }\n            else","sourceCodeStart":128,"sourceCodeEnd":164,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Collections/ObjectModel/WeakReadOnlyCollection.cs#L128-L164","documentation":"CopyTo only supports zero-based arrays; the library throws ArgumentException with SR.Arg_NonZeroLowerBound when array.GetLowerBound(0) != 0. Non-zero-lower-bound arrays ( creatable via Array.CreateInstance with custom bounds) have no single correct mapping for the flat index parameter.","triggerScenarios":"Calling CopyTo passing an array created with Array.CreateInstance(typeof(WeakReference), lengths, lowerBounds) where the first lower bound is not 0.","commonSituations":"Interop with COM/legacy code that uses non-zero-based arrays; code ported from VB with Option Base-style arrays.","solutions":["Use a standard zero-based array (new T[n] or Array.CreateInstance without lowerBounds)","Copy into a zero-based array first, then Array.Copy into the custom-bounded array","Guard with array.GetLowerBound(0) == 0 before calling"],"exampleFix":"// before\nvar arr = Array.CreateInstance(typeof(WeakReference), new[]{count}, new[]{1});\ncollection.CopyTo(arr, 0);\n// after\nvar arr = new WeakReference[count];\ncollection.CopyTo(arr, 0);","handlingStrategy":"validation","validationCode":"if (array.GetLowerBound(0) != 0) throw new ArgumentException(\"CopyTo requires a zero-based array.\", nameof(array));","typeGuard":"bool IsZeroBased(Array a) => a.GetLowerBound(0) == 0;","tryCatchPattern":"try { ((ICollection)collection).CopyTo(array, index); } catch (ArgumentException) { var tmp = new T[collection.Count]; ((ICollection)collection).CopyTo(tmp, 0); Array.Copy(tmp, 0, array, index, tmp.Length); }","preventionTips":["Avoid Array.CreateInstance with custom lowerBounds in modern code","Normalize external/COM arrays to zero-based before copying","Assert GetLowerBound(0) == 0 in utility copy methods"],"tags":["wpf","copyto","array-bounds"],"backgroundTag":"invalid-argument-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}