{"record":{"id":"5927dea492169125","repo":"dotnet/wpf","slug":"sr-invalidstyluspointconstructionzerolengthcollection","errorCode":null,"errorMessage":"SR.InvalidStylusPointConstructionZeroLengthCollection","messagePattern":"SR\\.InvalidStylusPointConstructionZeroLengthCollection","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Stylus/Common/StylusPointCollection.cs","lineNumber":45,"sourceCode":"\n        /// <summary>\n        /// StylusPointCollection\n        /// </summary>\n        public StylusPointCollection()\n        {\n            _stylusPointDescription = new StylusPointDescription();\n        }\n\n        /// <summary>\n        /// StylusPointCollection\n        /// </summary>\n        /// <param name=\"initialCapacity\">initialCapacity</param>\n        public StylusPointCollection(int initialCapacity) \n            : this()\n        {\n            if (initialCapacity < 0)\n            {\n                throw new ArgumentException(SR.InvalidStylusPointConstructionZeroLengthCollection, nameof(initialCapacity));\n            }\n            ((List<StylusPoint>)this.Items).Capacity = initialCapacity;\n        }\n\n        /// <summary>\n        /// StylusPointCollection\n        /// </summary>\n        /// <param name=\"stylusPointDescription\">stylusPointDescription</param>\n        public StylusPointCollection(StylusPointDescription stylusPointDescription)\n        {\n            ArgumentNullException.ThrowIfNull(stylusPointDescription);\n            _stylusPointDescription = stylusPointDescription;\n        }\n\n        /// <summary>\n        /// StylusPointCollection\n        /// </summary>\n        /// <param name=\"stylusPointDescription\">stylusPointDescription</param>","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Stylus/Common/StylusPointCollection.cs#L27-L63","documentation":"StylusPointCollection's (int initialCapacity) constructor throws ArgumentException when initialCapacity is negative. The SR string name is misleading; the check rejects negative capacity, not zero-length collections. The library requires a non-negative starting capacity because it maps to List<StylusPoint>.Capacity.","triggerScenarios":"Calling new StylusPointCollection(negativeNumber), typically when the capacity is computed from a size/count expression that underflows (e.g. count - extra) or comes from unvalidated external input.","commonSituations":"Computing capacity from an int arithmetic result that went negative; passing a default -1 sentinel; porting code where an unsigned value became negative after conversion.","solutions":["Ensure the value passed as initialCapacity is >= 0 before constructing","Clamp with Math.Max(0, initialCapacity) when the value is computed","Fix the upstream arithmetic that produces a negative count"],"exampleFix":"// before\nvar spc = new StylusPointCollection(count - margin);\n// after\nvar spc = new StylusPointCollection(Math.Max(0, count - margin));","handlingStrategy":"validation","validationCode":"if (initialCapacity < 0) throw new ArgumentOutOfRangeException(nameof(initialCapacity));","typeGuard":"bool IsValidCapacity(int c) => c >= 0;","tryCatchPattern":"try { var spc = new StylusPointCollection(cap); } catch (ArgumentException ex) when (ex.ParamName == \"initialCapacity\") { /* fall back to parameterless ctor */ }","preventionTips":["Clamp computed capacities with Math.Max(0, value)","Never pass sentinel values like -1 as capacity"],"tags":["wpf","stylus","argument-validation"],"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"}