dotnet/wpf · error · ArgumentException

SR.InvalidStylusPointConstructionZeroLengthCollection

Error message

SR.InvalidStylusPointConstructionZeroLengthCollection

What it means

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.

Solutions

  1. Ensure the value passed as initialCapacity is >= 0 before constructing
  2. Clamp with Math.Max(0, initialCapacity) when the value is computed
  3. Fix the upstream arithmetic that produces a negative count

Example fix

// before
var spc = new StylusPointCollection(count - margin);
// after
var spc = new StylusPointCollection(Math.Max(0, count - margin));
Defensive patterns

Strategy: validation

Validate before calling

if (initialCapacity < 0) throw new ArgumentOutOfRangeException(nameof(initialCapacity));

Type guard

bool IsValidCapacity(int c) => c >= 0;

Try / catch

try { var spc = new StylusPointCollection(cap); } catch (ArgumentException ex) when (ex.ParamName == "initialCapacity") { /* fall back to parameterless ctor */ }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/5927dea492169125. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Stylus/Common/StylusPointCollection.cs:45

        /// <summary>
        /// StylusPointCollection
        /// </summary>
        public StylusPointCollection()
        {
            _stylusPointDescription = new StylusPointDescription();
        }

        /// <summary>
        /// StylusPointCollection
        /// </summary>
        /// <param name="initialCapacity">initialCapacity</param>
        public StylusPointCollection(int initialCapacity) 
            : this()
        {
            if (initialCapacity < 0)
            {
                throw new ArgumentException(SR.InvalidStylusPointConstructionZeroLengthCollection, nameof(initialCapacity));
            }
            ((List<StylusPoint>)this.Items).Capacity = initialCapacity;
        }

        /// <summary>
        /// StylusPointCollection
        /// </summary>
        /// <param name="stylusPointDescription">stylusPointDescription</param>
        public StylusPointCollection(StylusPointDescription stylusPointDescription)
        {
            ArgumentNullException.ThrowIfNull(stylusPointDescription);
            _stylusPointDescription = stylusPointDescription;
        }

        /// <summary>
        /// StylusPointCollection
        /// </summary>
        /// <param name="stylusPointDescription">stylusPointDescription</param>

View on GitHub (pinned to 81131a70a4)