{"record":{"id":"bfd168424f4e840b","repo":"dotnet/wpf","slug":"sr-emptysctoreplacewith","errorCode":null,"errorMessage":"SR.EmptyScToReplaceWith","messagePattern":"SR\\.EmptyScToReplaceWith","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Ink/StrokeCollection.cs","lineNumber":469,"sourceCode":"            StrokeCollection strokesToReplace = new StrokeCollection();\n            strokesToReplace.Add(strokeToReplace);\n            this.Replace(strokesToReplace, strokesToReplaceWith);\n        }\n\n        /// <summary>\n        /// Replace\n        /// </summary>\n        /// <param name=\"strokesToReplace\"></param>\n        /// <param name=\"strokesToReplaceWith\"></param>\n        public void Replace(StrokeCollection strokesToReplace, StrokeCollection strokesToReplaceWith)\n        {\n            if ( strokesToReplace == null )\n            {\n                throw new ArgumentNullException(SR.EmptyScToReplace);\n            }\n            if ( strokesToReplaceWith == null )\n            {\n                throw new ArgumentNullException(SR.EmptyScToReplaceWith);\n            }\n\n            int replaceCount = strokesToReplace.Count;\n            if ( replaceCount == 0 )\n            {\n                ArgumentException ae = new ArgumentException(SR.EmptyScToReplace, nameof(strokesToReplace));\n                //\n                // we add a tag here so we can check for this in EraserBehavior.OnPointEraseResultChanged\n                // to determine if this method is the origin of an ArgumentException we harden against\n                //\n                ae.Data.Add(\"System.Windows.Ink.StrokeCollection\", \"\");\n                throw ae;\n            }\n\n            int[] indexes = this.GetStrokeIndexes(strokesToReplace);\n            if ( indexes == null )\n            {\n                // At least one stroke doesn't exist in our collection. We throw.","sourceCodeStart":451,"sourceCodeEnd":487,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Ink/StrokeCollection.cs#L451-L487","documentation":"StrokeCollection.Replace(StrokeCollection, StrokeCollection) throws ArgumentException with SR.EmptyScToReplace when strokesToReplace is non-null but empty (Count == 0). An empty replacement set identifies no strokes, so the replace operation is undefined and rejected.","triggerScenarios":"Calling Replace with a StrokeCollection constructed but never populated (new StrokeCollection()), or with a filtered subset that ended up empty (e.g. selected strokes after the selection was cleared).","commonSituations":"Replace-on-selection workflows where InkCanvas.GetSelectedStrokes() returned an empty collection; building the replace set inside a loop that never executed due to a predicate mismatch.","solutions":["Guard with if (strokesToReplace.Count > 0) before calling Replace.","Only invoke Replace when a prior operation (selection, hit test) actually produced strokes.","Catch ArgumentException and treat it as a no-op in idempotent UI flows."],"exampleFix":"// before\nstrokes.Replace(selectedStrokes, replacement); // selectedStrokes.Count == 0\n// after\nif (selectedStrokes.Count > 0)\n    strokes.Replace(selectedStrokes, replacement);","handlingStrategy":"validation","validationCode":"if (strokesToReplace != null && strokesToReplace.Count > 0)\n    strokes.Replace(strokesToReplace, strokesToReplaceWith);","typeGuard":"static bool IsNonEmpty(StrokeCollection c) => c != null && c.Count > 0;","tryCatchPattern":"try { strokes.Replace(strokesToReplace, with); }\ncatch (ArgumentException ex) when (ex.ParamName == \"strokesToReplace\") { /* empty set: no-op */ }","preventionTips":["Check Count > 0 on selection-derived collections before Replace.","Re-check selection state immediately before replacing (it may have been cleared by a UI event).","Treat empty replace sets as idempotent no-ops in UI code."],"tags":["wpf","ink","empty-collection","replace"],"backgroundTag":"empty-required-field","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"}