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
- 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).
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
- Sort homogeneous List<BestDimension> collections only
- Compare DimensionKey values directly instead of routing foreign objects through CompareTo
- Add pattern-matching guards in custom comparers
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
- result must be of type
- string.Format(Strings.MismatchedValueType, typeof
- Strings.BadColumnCast (formatted with column.DataType…
- Strings.BadColumnCast (formatted with column.DataType…
- Strings.BadColumnCast (formatted with column.DataType…
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)