{"record":{"id":"2bb4d091b2f6e6cf","repo":"dotnet/wpf","slug":"sr-format-sr-tablecollectionelementtypeexpected-nameof","errorCode":null,"errorMessage":"SR.Format(SR.TableCollectionElementTypeExpected, nameof(TableColumn))","messagePattern":"SR\\.Format\\(SR\\.TableCollectionElementTypeExpected, nameof\\(TableColumn\\)\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TableColumnCollection.cs","lineNumber":278,"sourceCode":"        }\n\n        #endregion Public Methods\n\n        //-------------------------------------------------------------------\n        //\n        //  IList Members\n        //\n        //-------------------------------------------------------------------\n\n        #region IList Members\n\n        int IList.Add(object value)\n        {\n            TableColumn item = value as TableColumn;\n\n            if (item == null)\n            {\n                throw new ArgumentException(SR.Format(SR.TableCollectionElementTypeExpected, nameof(TableColumn)), nameof(value));\n            }\n\n            return ((IList)_columnCollection).Add(value);\n        }\n\n        void IList.Clear()\n        {\n            this.Clear();\n        }\n\n        bool IList.Contains(object value)\n        {\n            return ((IList)_columnCollection).Contains(value);\n        }\n\n        int IList.IndexOf(object value)\n        {\n            return ((IList)_columnCollection).IndexOf(value);","sourceCodeStart":260,"sourceCodeEnd":296,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TableColumnCollection.cs#L260-L296","documentation":"TableColumnCollection's explicit IList.Add only accepts TableColumn instances; passing any other object throws ArgumentException (TableCollectionElementTypeExpected) naming TableColumn as the expected element type. This guards the strongly-typed collection against loosely-typed IList access.","triggerScenarios":"Calling (table.Columns as IList).Add(value) — or IList-typed Add — where value is null or not a TableColumn (e.g. a TableRow, string, or UIElement).","commonSituations":"Reflection or late-binding code adding columns via the non-generic IList interface; XAML/data-binding that supplies wrong child elements into Table.Columns.","solutions":["Cast/convert the value to TableColumn before adding","Use the strongly-typed TableColumnCollection.Add(TableColumn) API instead of IList.Add","Check 'value is TableColumn' before the call to fail gracefully"],"exampleFix":"// before\n((IList)table.Columns).Add(new TableRow()); // wrong element type\n// after\ntable.Columns.Add(new TableColumn());","handlingStrategy":"type-guard","validationCode":"if (value is TableColumn col) table.Columns.Add(col); else throw new ArgumentException(nameof(value));","typeGuard":"static bool IsTableColumn(object v) => v is TableColumn;","tryCatchPattern":"try { list.Add(value); } catch (ArgumentException ex) when (ex.ParamName == \"value\") { /* wrong element type */ }","preventionTips":["Use the strongly-typed Add(TableColumn) API","Avoid casting Columns to IList in generic code","Validate element types before reflective adds"],"tags":["wpf","table","argument","type-mismatch"],"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-21T21:30:21.729Z"}