{"record":{"id":"7d4ac508e01dbcbd","repo":"dotnet/machinelearning","slug":"object-is-not-a-bestdimension","errorCode":null,"errorMessage":"Object is not a BestDimension","messagePattern":"Object is not a BestDimension","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.ML.TimeSeries/RootCauseAnalyzer.cs","lineNumber":788,"sourceCode":"        internal Dictionary<string, int> PointDis;\n        internal double Gain;\n        internal double GainRatio;\n\n        public BestDimension()\n        {\n            AnomalyDis = new Dictionary<string, int>();\n            PointDis = new Dictionary<string, int>();\n        }\n\n        public int CompareTo(object obj)\n        {\n            if (obj == null) return 1;\n\n            BestDimension other = obj as BestDimension;\n            if (other != null)\n                return DimensionKey.CompareTo(other.DimensionKey);\n            else\n                throw new ArgumentException(\"Object is not a BestDimension\");\n        }\n    }\n\n    internal class RootCauseScore\n    {\n        internal double Surprise;\n        internal double ExplanatoryScore;\n\n        public RootCauseScore(double surprise, double explanatoryScore)\n        {\n            Surprise = surprise;\n            ExplanatoryScore = explanatoryScore;\n        }\n    }\n\n    internal class DimensionComparer : EqualityComparer<Dictionary<string, object>>\n    {\n        public override bool Equals(Dictionary<string, object> x, Dictionary<string, object> y)","sourceCodeStart":770,"sourceCodeEnd":806,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.ML.TimeSeries/RootCauseAnalyzer.cs#L770-L806","documentation":"BestDimension.CompareTo(object) implements IComparable by casting the argument to BestDimension. If the passed object is non-null but of a different type, it throws ArgumentException(\"Object is not a BestDimension\").","triggerScenarios":"Sorting or comparing BestDimension instances against objects of another type — e.g. calling CompareTo with a DimensionKey, a RootCause item, or an unrelated object inside List.Sort/Array.Sort comparators that mix element types.","commonSituations":"Custom sort logic over mixed RootCause result collections; reflection-based or generic comparisons passing boxed values of the wrong type; comparing results of the RootCauseAnalyzer across different API versions.","solutions":["Only pass BestDimension instances to CompareTo; extract the DimensionKey field if comparing against keys.","Use the strongly-typed comparison or sort List<BestDimension> directly so the comparer never receives foreign types.","Handle ArgumentException around comparisons over heterogeneous collections, or pre-filter with `is BestDimension`.","Fix custom comparer code to compare like-typed elements (e.g. sort by DimensionKey on both sides)."],"exampleFix":"// before\nobject other = someKey;\nbestDim.CompareTo(other); // ArgumentException\n// after\nif (other is BestDimension b) bestDim.CompareTo(b);\nelse bestDim.DimensionKey.CompareTo(other);","handlingStrategy":"type-guard","validationCode":"if (obj is not BestDimension) throw new InvalidOperationException(\"CompareTo expects BestDimension\");","typeGuard":"bool IsBestDimension(object o) => o is BestDimension;","tryCatchPattern":"try { cmp = a.CompareTo(b); }\ncatch (ArgumentException) { cmp = string.CompareOrdinal(a?.ToString(), b?.ToString()); }","preventionTips":["Sort homogeneous List<BestDimension> collections only","Compare DimensionKey values directly instead of routing foreign objects through CompareTo","Add pattern-matching guards in custom comparers"],"tags":["icomparable","argument-exception","root-cause","type-mismatch"],"backgroundTag":"invalid-argument-value","analyzedSha":"7b76e69cf964daeca3f1377af6bc5543284d56c6","analyzedAt":"2026-09-11T12:35:38.930Z","contentChangedAt":"2026-09-11T12:35:38.930Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}