{"record":{"id":"78be71aa1a46684f","repo":"dotnet/wpf","slug":"sr-format-sr-removerequirespositivecount-count","errorCode":null,"errorMessage":"SR.Format(SR.RemoveRequiresPositiveCount, count)","messagePattern":"SR\\.Format\\(SR\\.RemoveRequiresPositiveCount, count\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ItemContainerGenerator.cs","lineNumber":274,"sourceCode":"        }\n\n        /// <summary>\n        /// Remove generated elements.\n        /// </summary>\n        void IItemContainerGenerator.Remove(GeneratorPosition position, int count)\n        {\n            Remove(position, count, /*isRecycling = */ false);\n        }\n\n        /// <summary>\n        /// Remove generated elements.\n        /// </summary>\n        private void Remove(GeneratorPosition position, int count, bool isRecycling)\n        {\n            if (position.Offset != 0)\n                throw new ArgumentException(SR.Format(SR.RemoveRequiresOffsetZero, position.Index, position.Offset), nameof(position));\n            if (count <= 0)\n                throw new ArgumentException(SR.Format(SR.RemoveRequiresPositiveCount, count), nameof(count));\n\n            if (_itemMap == null)\n            {\n                // ignore reentrant call (during RemoveAllInternal)\n                Debug.Fail(\"Unexpected reentrant call to ICG.Remove\");\n                return;\n            }\n\n            int index = position.Index;\n            ItemBlock block;\n\n            // find the leftmost item to remove\n            int offsetL = index;\n            for (block = _itemMap.Next;  block != _itemMap;  block = block.Next)\n            {\n                if (offsetL < block.ContainerCount)\n                    break;\n","sourceCodeStart":256,"sourceCodeEnd":292,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ItemContainerGenerator.cs#L256-L292","documentation":"ItemContainerGenerator.Remove/Recycle validates that the count of containers to remove is positive. Passing zero or a negative count makes the operation meaningless and throws ArgumentException with SR.RemoveRequiresPositiveCount.","triggerScenarios":"Calling Remove(position, 0, ...) or Remove(position, negativeCount, ...), typically from a loop that computed count as lastIndex-firstIndex when lastIndex < firstIndex.","commonSituations":"Custom virtualization code computing removal ranges from viewport math that produced an inverted or empty range; off-by-one errors when the range is empty.","solutions":["Guard the call: only invoke Remove when count > 0.","Fix range math so lastIndex >= firstIndex before computing count = lastIndex - firstIndex + 1.","Skip the Remove call entirely for empty ranges instead of passing 0."],"exampleFix":"// before\ngenerator.Remove(position, count, true); // count may be 0\n\n// after\nif (count > 0)\n    generator.Remove(position, count, true);","handlingStrategy":"validation","validationCode":"if (count <= 0) return; // nothing to remove\ngenerator.Remove(position, count, true);","typeGuard":"static bool IsValidRemoveCount(int c) => c > 0;","tryCatchPattern":"try { generator.Remove(pos, count, true); }\ncatch (ArgumentException ex) when (ex.ParamName == \"count\") { /* log empty/negative range */ }","preventionTips":["Compute count as (last - first + 1) only when last >= first.","Early-return for empty ranges instead of calling Remove with 0."],"tags":["wpf","itemcontainergenerator","argument","range"],"backgroundTag":"argument-out-of-range","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"}