dotnet/wpf · error · ArgumentException

SR.Collection_BadType (double)

Error message

SR.Collection_BadType (double)

What it means

DoubleCollection.Cast is the private type-conversion helper invoked from Add and Insert. When the value boxed in an object/IList slot is not a System.Double, it throws ArgumentException with SR.Collection_BadType stating the expected element type ('double'). The collection only stores doubles, so non-double elements are rejected at insertion time.

Solutions

  1. Convert the value to double before adding: collection.Add(Convert.ToDouble(value)).
  2. Cast or parse to System.Double explicitly when going through the IList interface.
  3. If the element is genuinely another numeric type, use the appropriate collection (e.g. Int32Collection) instead.

Example fix

// before
((IList)coll).Add(42); // int boxed → ArgumentException Collection_BadType
// after
((IList)coll).Add(42.0); // double boxed, accepted
Defensive patterns

Strategy: type-guard

Validate before calling

if (value is not double)
    throw new ArgumentException($"Expected double, got {value?.GetType().Name}");

Type guard

static bool IsDouble(object v) => v is double;

Try / catch

try { ((IList)coll).Add(value); }
catch (ArgumentException ex) when (ex.Message.Contains("Collection_BadType"))
{ coll.Add(Convert.ToDouble(value)); }

Prevention

When it happens

Trigger: Calling ((IList)doubleCollection).Add(value) or ((IList)doubleCollection).Insert(index, value) with a non-double boxed value such as an int, float, decimal, or string. Note the implicit-typed Add(double) / Insert(int, double) overloads cannot trigger this — only the non-generic IList surface can.

Common situations: Legacy IList-based code or data binding adding parsed values (e.g. int or float from configuration) into a DoubleCollection through the non-generic interface; refactoring changing variable types from double to float/int.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/DoubleCollection.cs:455

        /// </summary>
        internal double Internal_GetItem(int i)
        {
            return _collection[i];
        }



        #endregion

        #region Private Helpers

        private double Cast(object value)
        {
            ArgumentNullException.ThrowIfNull(value);

            if (!(value is double))
            {
                throw new System.ArgumentException(SR.Format(SR.Collection_BadType, this.GetType().Name, value.GetType().Name, "double"));
            }

            return (double) value;
        }

        // IList.Add returns int and IList<T>.Add does not. This
        // is called by both Adds and IList<T>'s just ignores the
        // integer
        private int AddHelper(double value)
        {
            int index = AddWithoutFiringPublicEvents(value);

            // AddAtWithoutFiringPublicEvents incremented the version

            WritePostscript();

            return index;
        }

View on GitHub (pinned to 81131a70a4)