Skip to content

Type implementing IComparable<float> (or any IComparable) fails comparison. #3641

Description

@manfred-brands

Giving the following code:

private struct C : IComparable<float>
{
    public C(float v) => this.V = v;
    public float V { get; }
    public int CompareTo(float other) => this.V.CompareTo(other);
}

public void TestMethod()
{
    C x = new C(1.1f);
    float y = 1.0f;

    Assert.That(x.CompareTo(y), Is.GreaterThan(0));
    Assert.That(x,Is.GreaterThan(y));
}

The last test fails because NunitComparer.Compare checks y is IComparable before checking for xType.CompareTo(yType) and hence calls the Single.CompareTo(Object) method instead of C.CompareTo(float) method.

Can I switch the order such that the two tests on IComparable are last thereby preferencing IComparable<T> over IComparable?
I you think there are no unforeseen issues, I will raise a PR.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions