{"record":{"id":"f60ee68717b68ec0","repo":"dotnet/wpf","slug":"sr-collection-badtype-double","errorCode":null,"errorMessage":"SR.Collection_BadType (double)","messagePattern":"SR\\.Collection_BadType \\(double\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/DoubleCollection.cs","lineNumber":455,"sourceCode":"        /// </summary>\n        internal double Internal_GetItem(int i)\n        {\n            return _collection[i];\n        }\n\n\n\n        #endregion\n\n        #region Private Helpers\n\n        private double Cast(object value)\n        {\n            ArgumentNullException.ThrowIfNull(value);\n\n            if (!(value is double))\n            {\n                throw new System.ArgumentException(SR.Format(SR.Collection_BadType, this.GetType().Name, value.GetType().Name, \"double\"));\n            }\n\n            return (double) value;\n        }\n\n        // IList.Add returns int and IList<T>.Add does not. This\n        // is called by both Adds and IList<T>'s just ignores the\n        // integer\n        private int AddHelper(double value)\n        {\n            int index = AddWithoutFiringPublicEvents(value);\n\n            // AddAtWithoutFiringPublicEvents incremented the version\n\n            WritePostscript();\n\n            return index;\n        }","sourceCodeStart":437,"sourceCodeEnd":473,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/DoubleCollection.cs#L437-L473","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Convert the value to double before adding: collection.Add(Convert.ToDouble(value)).","Cast or parse to System.Double explicitly when going through the IList interface.","If the element is genuinely another numeric type, use the appropriate collection (e.g. Int32Collection) instead."],"exampleFix":"// before\n((IList)coll).Add(42); // int boxed → ArgumentException Collection_BadType\n// after\n((IList)coll).Add(42.0); // double boxed, accepted","handlingStrategy":"type-guard","validationCode":"if (value is not double)\n    throw new ArgumentException($\"Expected double, got {value?.GetType().Name}\");","typeGuard":"static bool IsDouble(object v) => v is double;","tryCatchPattern":"try { ((IList)coll).Add(value); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"Collection_BadType\"))\n{ coll.Add(Convert.ToDouble(value)); }","preventionTips":["Use the generic Add(double)/Insert(int,double) overloads rather than IList.","Convert.Parse/Convert.ToDouble values coming from config or parsing before insertion.","Watch for implicit boxing of int/float literals passed through object-typed APIs."],"tags":["wpf","collection","type-mismatch","ilist"],"backgroundTag":"type-mismatch","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"}