{"record":{"id":"25eaf6c046102b59","repo":"dotnet/wpf","slug":"sr-size-widthandheightcannotbenegative","errorCode":null,"errorMessage":"SR.Size_WidthAndHeightCannotBeNegative","messagePattern":"SR\\.Size_WidthAndHeightCannotBeNegative","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Rect.cs","lineNumber":48,"sourceCode":"                _x = location._x;\n                _y = location._y;\n                _width = size._width;\n                _height = size._height;\n            }\n        }\n\n        /// <summary>\n        /// Constructor which sets the initial values to the values of the parameters.\n        /// Width and Height must be non-negative\n        /// </summary>\n        public Rect(double x,\n                    double y,\n                    double width,\n                    double height)\n        {\n            if (width < 0 || height < 0)\n            {\n                throw new System.ArgumentException(SR.Size_WidthAndHeightCannotBeNegative);\n            }\n\n            _x    = x;\n            _y     = y;\n            _width   = width;\n            _height  = height;\n        }\n\n        /// <summary>\n        /// Constructor which sets the initial values to bound the two points provided.\n        /// </summary>\n        public Rect(Point point1,\n                    Point point2)\n        {\n            _x = Math.Min(point1._x, point2._x);\n            _y = Math.Min(point1._y, point2._y);\n\n            //  Max with 0 to prevent double weirdness from causing us to be (-epsilon..0)","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Rect.cs#L30-L66","documentation":"The Rect(double x, double y, double width, double height) constructor validates that neither dimension is negative and throws ArgumentException otherwise. WPF's Rect models an axis-aligned rectangle whose width and height must be zero or positive; the special 'empty' rect is represented by NaN fields, not by negative sizes. This guard exists so invariants in layout and geometry code never see malformed rectangles.","triggerScenarios":"Calling new Rect(x, y, width, height) where width < 0 or height < 0, e.g. computing width as right-left without clamping negative results.","commonSituations":"Subtracting coordinates in the wrong order (right - left where right < left), deserializing sizes from untrusted data, porting GDI/WinForms code where negative width meant 'flip', or computing span from swapped points without Point order normalization.","solutions":["Compute width/height as Math.Abs(right - left) / Math.Abs(bottom - top), or use the Rect(Point, Point) constructor which normalizes points automatically.","Validate inputs before constructing: if (width < 0 || height < 0) throw or clamp to 0.","If an empty rect is intended, use Rect.Empty instead of a negative-size rect."],"exampleFix":"// before\nvar rect = new Rect(left, top, right - left, bottom - top);\n// after\nvar rect = new Rect(Math.Min(left, right), Math.Min(top, bottom),\n                    Math.Abs(right - left), Math.Abs(bottom - top));","handlingStrategy":"validation","validationCode":"public static bool IsValidExtent(double width, double height) => width >= 0 && height >= 0;\n// then: if (!IsValidExtent(w, h)) w = Math.Abs(w); h = Math.Abs(h);\n// or simply: var rect = new Rect(new Point(x, y), new Point(x2, y2)); // normalizes automatically","typeGuard":"static bool CanBuildRect(double w, double h) => !double.IsNaN(w) && !double.IsNaN(h) && w >= 0 && h >= 0;","tryCatchPattern":"try { var rect = new Rect(x, y, w, h); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"negative\")) { /* normalize points and retry */ }","preventionTips":["Prefer the Rect(Point, Point) or Rect(Point, Size) constructors over raw width/height math.","Always derive width/height with Math.Abs or Math.Min/Max on corner points.","Never represent 'empty' or 'flipped' with negative sizes; use Rect.Empty or point swap."],"tags":["wpf","geometry","argument-validation","rect"],"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-22T01:17:13.364Z"}