stride3d/stride · error · ArgumentException

Argument must be of type Angle.

Error message

Argument must be of type Angle.

What it means

AngleSingle.CompareTo(object) requires the boxed argument to be an AngleSingle; any other non-null type causes an ArgumentException. This is the non-generic IComparable implementation used for ordering angles.

Solutions

  1. Pass an AngleSingle: use the generic IComparable<AngleSingle> (CompareTo(Angle)) instead of the object overload.
  2. Convert floats first: new AngleSingle(value, AngleType.Radian).CompareTo(other).
  3. Use typed collections/comparers (List<AngleSingle>, Comparer<AngleSingle>.Default).
  4. In reflection/binding scenarios, type-check with 'is AngleSingle' before calling CompareTo.

Example fix

// before
object a = 1.5f;
angle.CompareTo(a); // ArgumentException
// after
angle.CompareTo(new AngleSingle(1.5f, AngleType.Radian));
Defensive patterns

Strategy: type-guard

Validate before calling

if (other is not AngleSingle)
    throw new ArgumentException($"Expected AngleSingle, got {other?.GetType().Name}", nameof(other));

Type guard

static bool IsAngle(object? o) => o is AngleSingle;

Try / catch

try { cmp = angle.CompareTo(boxed); }
catch (ArgumentException) { cmp = angle.CompareTo(Convert.ToSingle(boxed).ToRadianAngle()); }

Prevention

When it happens

Trigger: Passing a boxed value of another type (float, double, or a different struct) to CompareTo, e.g. through sorting with a non-generic IComparer, reflection, or comparing an Angle with a plain number.

Common situations: Legacy IComparable-based collections (ArrayList, Array.Sort without comparer) mixing types; data binding frameworks boxing values; converting code that previously used raw floats.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/4b79f097faf92fd6. Report an issue: GitHub.

Appendix: source

Thrown at sources/core/Stride.Core.Mathematics/AngleSingle.cs:626

    /// Compares this instance to a specified object and returns an integer that
    /// indicates whether the value of this instance is less than, equal to, or greater
    /// than the value of the specified object.
    /// </summary>
    /// <param name="other">The object to compare.</param>
    /// <returns>
    /// A signed integer that indicates the relationship of the current instance
    /// to the obj parameter. If the value is less than zero, the current instance
    /// is less than the other. If the value is zero, the current instance is equal
    /// to the other. If the value is greater than zero, the current instance is
    /// greater than the other.
    /// </returns>
    public readonly int CompareTo(object? other)
    {
        if (other == null)
            return 1;

        if (other is not AngleSingle single)
            throw new ArgumentException("Argument must be of type Angle.", nameof(other));

        float radians = single.Radians;

        if (Radians > radians)
            return 1;

        if (Radians < radians)
            return -1;

        return 0;
    }

    /// <summary>
    /// Compares this instance to a second Stride.Core.Mathematics.AngleSingle and returns
    /// an integer that indicates whether the value of this instance is less than,
    /// equal to, or greater than the value of the specified object.
    /// </summary>
    /// <param name="other">The object to compare.</param>

View on GitHub (pinned to 96fad776d2)