{"record":{"id":"8be25bea24b579cd","repo":"dotnet/wpf","slug":"sr-size-cannotmodifyemptysize","errorCode":null,"errorMessage":"SR.Size_CannotModifyEmptySize","messagePattern":"SR\\.Size_CannotModifyEmptySize","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Size.cs","lineNumber":76,"sourceCode":"            {\n                return _width < 0;\n            }\n        }\n\n        /// <summary>\n        /// Width - Default is 0, must be non-negative\n        /// </summary>\n        public double Width\n        {\n            get\n            {\n                return _width;\n            }\n            set\n            {\n                if (IsEmpty)\n                {\n                    throw new System.InvalidOperationException(SR.Size_CannotModifyEmptySize);\n                }\n                    \n                if (value < 0)\n                {\n                    throw new System.ArgumentException(SR.Size_WidthCannotBeNegative);\n                }\n\n                _width = value;\n            }\n        }\n\n        /// <summary>\n        /// Height - Default is 0, must be non-negative.\n        /// </summary>\n        public double Height\n        {\n            get\n            {","sourceCodeStart":58,"sourceCodeEnd":94,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Size.cs#L58-L94","documentation":"The Size.Width setter throws InvalidOperationException when the Size is Empty. Size.Empty is a sentinel (width/height = NegativeInfinity) and cannot be mutated; you must replace it with a real Size first.","triggerScenarios":"Assigning size.Width = x where size == Size.Empty, e.g. a field initialized to Size.Empty and mutated one dimension at a time.","commonSituations":"Incrementally building a size on a control whose size field defaulted to Size.Empty; lazily-initialized layout state mutated before first measure.","solutions":["Check IsEmpty and assign a whole new Size instead of setting one property","Initialize fields with new Size(0, 0) rather than Size.Empty when incremental mutation is intended","Use size = new Size(width, size.IsEmpty ? 0 : size.Height) replacement pattern"],"exampleFix":"// before\nif (size.IsEmpty) size.Width = 100; // throws\n// after\nsize = size.IsEmpty ? new Size(100, 0) : new Size(100, size.Height);","handlingStrategy":"validation","validationCode":"if (size.IsEmpty) size = new Size(width, 0); else size = new Size(width, size.Height);","typeGuard":"bool CanSetDimension(Size s) => !s.IsEmpty;","tryCatchPattern":"try { size.Width = w; } catch (InvalidOperationException) { size = new Size(w, 0); }","preventionTips":["Initialize size fields with new Size(0,0) if mutated incrementally","Treat Size.Empty as immutable; always replace whole instances","Centralize size mutation in a helper that handles Empty"],"tags":["wpf","size","invalid-operation","empty-size"],"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"}