dotnet/machinelearning · error · ArgumentException

Object is not a BestDimension

Error message

Object is not a BestDimension

What it means

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").

Solutions

  1. Only pass BestDimension instances to CompareTo; extract the DimensionKey field if comparing against keys.
  2. Use the strongly-typed comparison or sort List<BestDimension> directly so the comparer never receives foreign types.
  3. Handle ArgumentException around comparisons over heterogeneous collections, or pre-filter with `is BestDimension`.
  4. Fix custom comparer code to compare like-typed elements (e.g. sort by DimensionKey on both sides).

Example fix

// before
object other = someKey;
bestDim.CompareTo(other); // ArgumentException
// after
if (other is BestDimension b) bestDim.CompareTo(b);
else bestDim.DimensionKey.CompareTo(other);
Defensive patterns

Strategy: type-guard

Validate before calling

if (obj is not BestDimension) throw new InvalidOperationException("CompareTo expects BestDimension");

Type guard

bool IsBestDimension(object o) => o is BestDimension;

Try / catch

try { cmp = a.CompareTo(b); }
catch (ArgumentException) { cmp = string.CompareOrdinal(a?.ToString(), b?.ToString()); }

Prevention

When it happens

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

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

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/machinelearning@7b76e69cf9 (2026-09-11). Data as JSON: /api/errors/7d4ac508e01dbcbd. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.ML.TimeSeries/RootCauseAnalyzer.cs:788

        internal Dictionary<string, int> PointDis;
        internal double Gain;
        internal double GainRatio;

        public BestDimension()
        {
            AnomalyDis = new Dictionary<string, int>();
            PointDis = new Dictionary<string, int>();
        }

        public int CompareTo(object obj)
        {
            if (obj == null) return 1;

            BestDimension other = obj as BestDimension;
            if (other != null)
                return DimensionKey.CompareTo(other.DimensionKey);
            else
                throw new ArgumentException("Object is not a BestDimension");
        }
    }

    internal class RootCauseScore
    {
        internal double Surprise;
        internal double ExplanatoryScore;

        public RootCauseScore(double surprise, double explanatoryScore)
        {
            Surprise = surprise;
            ExplanatoryScore = explanatoryScore;
        }
    }

    internal class DimensionComparer : EqualityComparer<Dictionary<string, object>>
    {
        public override bool Equals(Dictionary<string, object> x, Dictionary<string, object> y)

View on GitHub (pinned to 7b76e69cf9)