{"record":{"id":"f1867c926989462a","repo":"dotnet/wpf","slug":"sr-rect-cannotmodifyemptyrect","errorCode":null,"errorMessage":"SR.Rect_CannotModifyEmptyRect","messagePattern":"SR\\.Rect_CannotModifyEmptyRect","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Rect.cs","lineNumber":148,"sourceCode":"\n                return _width < 0;\n            }\n        }\n\n        /// <summary>\n        /// Location - The Point representing the origin of the Rectangle\n        /// </summary>\n        public Point Location\n        {\n            get\n            {\n                return new Point(_x, _y);\n            }\n            set\n            {\n                if (IsEmpty)\n                {\n                    throw new System.InvalidOperationException(SR.Rect_CannotModifyEmptyRect);\n                }\n                \n                _x = value._x;\n                _y = value._y;\n            }\n        }\n\n        /// <summary>\n        /// Size - The Size representing the area of the Rectangle\n        /// </summary>\n        public Size Size\n        {\n            get\n            {\n                if (IsEmpty)\n                    return Size.Empty;\n                return new Size(_width, _height);\n            }","sourceCodeStart":130,"sourceCodeEnd":166,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Rect.cs#L130-L166","documentation":"Rect.Location's setter throws InvalidOperationException when the rect is the special Empty rect. The empty rect is a singleton (all fields NaN) with no defined position, so modifying its location is meaningless and would corrupt the sentinel representation. WPF exposes this as an invalid-state error rather than silently assigning.","triggerScenarios":"Assigning rect.Location = new Point(...) while rect == Rect.Empty, typically after a default Rect field or an Intersection/Union that produced Empty.","commonSituations":"Storing a Rect in a class field initialized to default(Rect) or Rect.Empty and later setting Location directly; hit-test results returning Empty when nothing was hit; layout code that forgot to initialize a rect.","solutions":["Assign a whole new Rect instead of mutating: rect = new Rect(point, size).","Check rect.IsEmpty before setting Location and handle the empty case explicitly.","Initialize fields with a real rect (e.g. Rect(0, 0, w, h)) instead of Rect.Empty/default."],"exampleFix":"// before\nif (hitRect.IsEmpty) { hitRect.Location = origin; } // throws\n// after\nif (!hitRect.IsEmpty) { hitRect.Location = origin; } else { hitRect = new Rect(origin, hitSize); }","handlingStrategy":"validation","validationCode":"if (!rect.IsEmpty) { rect.Location = new Point(x, y); }","typeGuard":"static bool IsMutable(Rect r) => !r.IsEmpty;","tryCatchPattern":"try { rect.Location = p; }\ncatch (InvalidOperationException) { rect = new Rect(p, rect.Size == default ? new Size(0,0) : rect.Size); }","preventionTips":["Check IsEmpty before any per-field mutation of a Rect.","Avoid default(Rect) and Rect.Empty as mutable placeholders; initialize with real geometry.","Prefer constructing a new Rect over mutating Location/X/Y on shared values."],"tags":["wpf","rect","invalid-state","empty-rect"],"backgroundTag":"invalid-state-transition","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"}